Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculate conditional means only based on one column in R

Tags:

r

I have a dataframe like this:

block   plot    date    data
1   1   aug 11.95171507
1   1   aug 18.41451063
1   2   aug 9.506155236
1   2   aug 13.26259947
1   3   aug 17.53616835
1   3   sep 15.40950767
2   1   sep 23.03616678
2   1   sep 17.07067258
2   2   sep 11.58278798
2   2   sep 13.15443304

I would like to calculate the means of data across plot based on block and date. Eventually, i would like to have 5 means. Thanks for your help.

like image 542
Dan Avatar asked Dec 27 '22 05:12

Dan


1 Answers

Probably the path of least resistance is to use plyr:

library(plyr)
ddply(yourData, c("block", "date"), summarize, outVal = mean(data))

You can do similar things with data.table, aggregate, by and probably a whole host of other functions. Take a few minutes to peruse the R tag here on SO.

like image 79
Chase Avatar answered Jan 25 '23 23:01

Chase