I have a dataframe df
with plot names and values for species found on those plots:
df=data.frame(plot=c(1000, 1000, 1000, 1005, 1005, 1005, 1009, 1009, 1009), speciesA=c(5, 0.5, 10, 7, 8, 45, 0.2, 3, 17), speciesB = c(1, 11, 46, 98, 0.2, 14, 40, 37, 22), speciesC = c(0.7, 72, 17, 0, 14, 8, 0, 9, 0.9))
Now I want to find the mean for each species for each plot, resulting in a dataframe df2
like:
df2=data.frame(plot=c(1000,1005,1009), speciesA=c(5.166, 20, 20.2), speciesB=c(19.333, 37.4, 33), speciesC=c(89.7, 7.333, 3.3))
I've tried:
df2 <- df %>% group_by(plot) %>% summarise_each(funs(mean))
Which just made the program completely unresponsive. And I know that I can collapse the rows using:
df2 <- df[, lapply(.SD, paste0, collapse=""), by=plot]
But this just collapses all of the numbers together, I'm not sure how to use it to calculate mean. I've already tried searching here for answers but I apologize if this is still a duplicate question.