R: What are the best functions to deal with concatenating and averaging values in a data.frame?

前端 未结 3 788
时光取名叫无心
时光取名叫无心 2021-01-06 19:59

I have a data.frame from this code:

   my_df = data.frame(\"read_time\" = c(\"2010-02-15\", \"2010-02-15\", 
                                      \"2010-02-         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-06 20:19

    The plyr package is popular for this, but the base functions by() and aggregate() will also help.

    > ddply(my_df, "read_time", function(X) data.frame(OD=mean(X$OD),stdev=sd(X$OD)))
       read_time      OD   stdev
    1 2010-02-15 0.15000 0.07071
    2 2010-02-16 0.23333 0.15275
    3 2010-02-17 0.50000      NA
    

    You can add the missing bit to return 0 instead of NA for the last std.dev.

    Also, you don't need the quotes (on the variables) you had in the data.frame construction.

提交回复
热议问题