calculate conditional means only based on one column in R

后端 未结 3 526
北海茫月
北海茫月 2021-01-06 08:15

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条回答
  •  滥情空心
    2021-01-06 08:59

    Let's say that your data is stored in a data frame called "DATA".

    > DATA
    
       block plot date      data
    1      1    1  aug 11.951715
    2      1    1  aug 18.414511
    3      1    2  aug  9.506155
    4      1    2  aug 13.262599
    5      1    3  aug 17.536168
    6      1    3  sep 15.409508
    7      2    1  sep 23.036167
    8      2    1  sep 17.070673
    9      2    2  sep 11.582788
    10     2    2  sep 13.154433
    

    We can then type the following:

    > aggregate(DATA$data, list(Block = DATA$block, Date = DATA$date), mean)
    
      Block Date        x
    1     1  aug 14.13423
    2     1  sep 15.40951
    3     2  sep 16.21102
    

提交回复
热议问题