I'm attempting to calculate two summary statistics (mean and standard error) from the following data set, where both Location and Adult should be factors.
Location Adult OverComp
F 1 7
P 1 8
P 0 10
F 1 3
F 0 11
I would like the output to appear as follows:
Location Adult OverComp.m OverComp.se
F 1 (mean) (standard error)
F 0 (mean) (standard error)
P 1 (mean) (standard error)
P 0 (mean) (standard error)
Where OverComp.m is the calculated mean for each combination of Location x Adult, and OverComp.se is standard error for each of those combinations. I want this format because I want to then use this with ggplot2, to make a bar plot of the four means & se's, color-coded for Location.
I've gotten this far:
summary.OverComp <-data.frame(
+ Location=levels(as.factor(data$FLocation)),
+ MeanOverComp=tapply(data$OverComp, list(data$FLocation,data$Adult), mean),
+ se=tapply(data$OverComp, list(data$FLocation,data$Adult),std.error))
Which produces the statistics I want, but not the format that I need for plotting in ggplot2 (as far as I can tell):
summary.OverComp
Location MeanOverComp.0 MeanOverComp.1 se.0 se.1
F Fiji 7.238095 8.454545 0.3792062 0.3023071
P Peru 6.893617 5.395833 0.4544304 0.3076155
I am now a bit clueless - not sure whether to pursue a different method for plotting, or a transformation to the above output, or to figure out how to incorporate Adult as a factor in my summary coding. I have an inkling that reshape2 may be involved, but not sure how to approach that. Your help would be much appreciated!