0

I am trying a different package for this problem because I couldn't figure out dplyr. Here is my code using Rmisc package to plot the mean length by sex and region:

LCSummary=summarySE(LabCultures, measurevar = 'Length (mm)', groupvars = c('Region','Sex'))

However, I keep getting this error:

Error in [.data.frame(xx, , col) : undefined columns selected

When I knit the code using Rmarkdown, it says that it can't find the function summarySE.

Even after getting these errors, if I look at LCSummary, it gives me columns with sd, se, and ci. Does that mean it worked? I just wasn't sure what that first error message meant.

Thank you.

Here is the data:

dput(LabCultures)
structure(list(Region = c("South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "North", "North", "North", "North", "North", 
"North", "North", "North", "North", "North", "North", "North", 
"North", "North", "North", "North", "North", "North", "North", 
"North", "North", "North", "North", "North", "North", "North", 
"North", "North", "North", "North", "North", "North", "North", 
"North", "North", "North", "North", "North", "North", "North", 
"North", "North", "North", "North", "North", "North", "North", 
"North", "North", "North", "North", "North", "North", "North", 
"North", "North", "North", "North", "North", "North", "North", 
"North", "North", "North", "North", "North", "North", "North", 
"North", "North", "North", "North", "North", "North", "North", 
"North", "North", "North", "North", "North", "North", "North", 
"North", "North", "North", "North", "North", "North", "North", 
"North", "North", "North", "North", "North", "North", "North", 
"North", "North", "North", "North", "North", "North", "North", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "South", "South", "South", "South", 
"South"), Population = c("CCVA", "CCVA", "CCVA", "CCVA", "CCVA", 
"CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", 
"CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", 
"CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", 
"CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", 
"CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", "CCVA", 
"CCVA", "CCVA", "CCVA", "Magnolia", "Magnolia", "Magnolia", "Magnolia", 
"Magnolia", "Magnolia", "Magnolia", "Magnolia", "Magnolia", "Magnolia", 
"Magnolia", "Magnolia", "Magnolia", "Magnolia", "Magnolia", "Magnolia", 
"Magnolia", "Magnolia", "Magnolia", "Magnolia", "Magnolia", "Magnolia", 
"Magnolia", "Magnolia", "Magnolia", "Magnolia", "Magnolia", "Magnolia", 
"Magnolia", "Magnolia", "Magnolia", "Magnolia", "Magnolia", "Magnolia", 
"Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", 
"Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", 
"Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", 
"Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", 
"Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", 
"Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", 
"Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", 
"Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", 
"Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", 
"Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "Nahant", "VIMS", 
"VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", 
"VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", 
"VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", 
"VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", 
"VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", 
"VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", 
"VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", 
"VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", 
"VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", 
"VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", 
"VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", 
"VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", "VIMS", 
"VIMS", "VIMS"), Sex = c("F", "F", "F", "F", "F", "F", "F", "F", 
"F", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", 
"F", "F", "F", "F", "F", "F", "F", "F", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "F", "F", "F", "F", 
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", 
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", 
"F", "F", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", 
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", 
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", 
"F", "F", "F", "F", "F", "F", "F", "F", "F", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M"), Length..mm. = c(17, 
12.5, 14, 16, 13, 10, 11.5, 13, 10, 18, 18, 14, 18, 14, 12.5, 
20, 21, 17, 14.5, 17, 14, 15, 18, 19, 15, 21, 21, 22, 19, 14, 
18.5, 14, 15, 14, 16, 13.5, 17, 13, 14.5, 14, 16, 14, 14, 14, 
17, 17.5, 17, 21, 19, 19, 18.5, 15.5, 14, 18, 14, 19, 17, 15, 
15, 13, 13, 16, 15, 15, 15.5, 15.5, 16, 16.5, 18.5, 19.5, 22.5, 
16.5, 20.5, 17, 20, 24.5, 18, 14.5, 26.5, 19, 18.5, 13, 16, 16, 
15.5, 16, 16, 17, 18, 19, 17, 18, 15, 15, 16, 16, 16, 15, 13, 
14, 16, 15, 16, 14, 17, 14, 14, 14, 14, 14.5, 14.5, 13, 15, 14, 
22.5, 16, 20.5, 24, 16, 28, 24, 24, 21, 25, 23, 18.5, 21.5, 21, 
19, 21, 20, 18.5, 18, 18, 19, 15, 17, 17, 16, 15, 15, 15, 13, 
17, 15, 13, 17, 21, 16, 15, 15, 14.5, 17, 17, 14, 14, 17.5, 13, 
13.5, 15, 13.5, 17, 15, 14, 16, 15.5, 15, 14.5, 15, 14, 13, 14, 
12.5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 4.5, 3.5, 4, 4, 3.5, 4, 4, 3.5, 5, 
5.5, 5.5, 5, 5.5, 5, 6, 5, 4, 4.5, 4, 4.5, 4, 4.5, 6, 4, 5, 5, 
5, 4.5, 4.5, 4.5, 4, 4, 5, 4, 5, 3.5, 3.5, 3.5, 3, 3.5, 3.5, 
6.5, 4, 4, 3.5, 3.5, 4, 4, 4.5, 4, 5, 4.5, 5, 6, 3, 4, 5)), class = "data.frame", row.names = c(NA, 
-250L))
Basti
  • 1,703
  • 3
  • 10
  • 25
  • 1
    Could you add an exemple of your dataset `LabCultures` ? It is impossible to help you if we don't know what it corresponds to. You should add the output of `dput(LabCultures)` in your post – Basti Sep 01 '21 at 07:29
  • I'm sorry. I am new to this. I was actually able to figure that part out but now I am having trouble plotting it. – Alison Meeth Sep 01 '21 at 08:57
  • So far I have: ```{r,echo=TRUE} LabCultures<-read.csv('HW 1 Lab Cultures.csv') head(LabCultures) ddply(LabCultures, .(Sex, Region), summarize, mean = round(mean(Length..mm.), 2), sd = round(sd(Length..mm.), 2), se = round(sd(Length..mm.)/sqrt(length(Length..mm.)),2)) ``` ```{r,echo=TRUE} ggplot(LabCultures, aes(x=Region, y=Length..mm., color=Sex)) + geom_errorbar(aes(ymin=Length..mm.-se, ymax=Length..mm.+se), width=.1) + geom_line() + geom_point() + ggtitle("Mean Length by Sex and Region")+ylab("Length (mm)") ``` – Alison Meeth Sep 01 '21 at 08:57
  • But it says "se" is not found. But I thought I created that column in the code? – Alison Meeth Sep 01 '21 at 08:58
  • Could you have a look at this ? (part "Copying original data") : https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Indeed, we still need to know what your dataset looks like, as suggested in my precedent comment it would be great if you could edit your post by adding the output of `dput(LabCultures)` (it should be something that begins with `structure(...)`) – Basti Sep 01 '21 at 09:02
  • It won't let me edit. Hold on... – Alison Meeth Sep 01 '21 at 09:06
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Sep 01 '21 at 11:27

2 Answers2

1

From the code in your comment, it looks like you're not saving the added variables, as the output of the ddply command is not stored. In the ggplot command you get back to your original file, that does not contain the created variables. Furthermore, you should plot the new variables, not the original ones. Finally, I suggest to use the more current dplyr syntax. Try this:

library(dplyr)
library(ggplot2)
LabCultures <-read.csv('HW 1 Lab Cultures.csv') |>
  group_by(Sex, Region) |>
  summarise(mean = mean(Length..mm.), 
            sd = sd(Length..mm.),
            se = sd/sqrt(length(Length..mm.)),
            low = mean-se,
            hi = mean+se) |>
  ggplot(aes(x=Region, y=mean, color=Sex)) +    
     geom_errorbar(aes(ymin=low, ymax=hi), width=.1) + 
     geom_line() + 
     geom_point() + 
     ggtitle("Mean Length by Sex and Region")+
     ylab("Length (mm)") 

I could not read in your file, so I could not test the code on the actual data. I'm not sure what you'll get with the geom_line layer, you may need to add a group aesthetic.

Claudio
  • 1,229
  • 6
  • 11
1

First, I would recommend that you use colnames that are easy to use :

colnames(LabCultures)=c("Region","Population","Sex","Length (mm)","Width (mm)")

Then you can do your summary :

LCSummary=summarySE(LabCultures, measurevar = "Length (mm)", groupvars = c('Region','Sex'))

Finally, for your ggplot you need to take the summary and not your original dataset, we can imagine something like this :

ggplot(LCSummary, aes(x=Region, y=`Length (mm)`, color=Sex)) +    
  geom_errorbar(aes(ymin=`Length (mm)`-se, ymax=`Length (mm)`+se), width=.1) +   
  geom_line() +   
  geom_jitter(LabCultures,mapping=aes(x=Region,y=`Length (mm)`,color=Sex),width=0.1) + 
  ggtitle("Mean Length by Sex and Region")+
  ylab("Length (mm)")

enter image description here

Basti
  • 1,703
  • 3
  • 10
  • 25