##Script for calculation of means, standard deviations and errors of data set factor combinations
#tolerates missing values and computes standard error correctly based on counts of factor combinations
##Created by Larry Matthew York on 2-17-2012 for general use, have fun
##available at http://roots.psu.edu/en/rootstats/meansanderror

#example loading your data set from csv
#R must be set to working directory of csv file

#data is emergence as plants emerged in a row for low and high nitrogen levels and planted at two depths, C control and E experimental, deeper

emergenceDAY5 <- read.csv("emergenceDAY5.csv")

#use as.factor() now to save numeric factors as factors rather than numbers or script won't work, if you have that

######################################
####Change Parameters Below###########

#give name of full data set
fulldata <- emergenceDAY5

#omit missing values, code missing values as NA before loading into R, if necessary
#make sure you data set does not contain columns of unwanted NAs! these will make the whole row be erased by na.omit
#i had this problem when saving from excel as .csv, solved by highlighting only the data i wanted saved before saving!
fulldata <- na.omit(fulldata)

#give column name of response variable you're interested in
#this is done by changing the name of the column basically to make it generic
fulldata$meanResponse <- fulldata$EMERGE

#use this following if you need to know the names of your columns to use for factors
names(fulldata)

#give factors to aggregate by, don't include reps - that's what will be averaged
#this will give the combinations of the factor and average across reps, plugs in aggregagte()


meanfactors <- with(fulldata, list(NLEVEL,DEPTH))

# try meanfactors <- with(fulldata, list(DEPTH)) for comparison

#give names of columns
#the names() in the generic script code below is only needed if you want to rename, not required
namesMean <- c("Nlevel","Depth","mean")

##end of where you set parameters
##below the generic scrip following are options for output and graphing

############run this#######################
###Generic Script pulls from your input above

#use aggregate function to calculate the mean of your response based on factors
meanGeneric <- with(fulldata, aggregate(meanResponse, meanfactors, mean))
#use aggregate to count number of reponses for factor combination, gives reps for SE calculation
reps1 <- with(fulldata, aggregate(meanResponse, meanfactors, length))
#change column names to what you said
names(meanGeneric) <- namesMean
#use aggregate to caculate standard deviation based on factors, and add to sd column on object
meanGeneric$sd <- with(fulldata, aggregate(meanResponse, meanfactors, sd))$x
#calculate standard error based on sd and the reps counted above for factor combinations
meanGeneric$se <- meanGeneric$sd/sqrt(reps1$x)

###############END Generic Script##############################

##########save output to your own name

meanEmergenceDAY5 <- meanGeneric

##what were the results, anyways?  Just type the name of the object yous saved to:

meanEmergenceDAY5

####write to csv to change order and things if you want for GRAPHING!!!!

write.csv(meanGeneric, "meanEmergenceDAY5.csv")

###easy to graph!!!!
#from this simple graph you can build up to publication quality
#visit http://roots.psu.edu/en/rgraphs

barplot(meanGeneric$mean)

