Mean of variable by two factors

梦想与她 提交于 2019-12-03 03:43:30

Personally I would use aggregate:

aggregate(length ~ sp, data = df, FUN= "mean" )
# by species only
#     sp length
#1  1    5.0
#2  2    2.5

aggregate(length ~ sp + method, data = df, FUN= "mean" )
    # by species and method
#  sp method length
#1  1      A      4
#2  2      A      3
#3  1      B      6
#4  2      B      2

for everything together you may want:

aggregate(length ~ method, data = df, function(x) c(m = mean(x), counts = length(x)) )

# counts and mean for each method
#  method length.m length.counts
#1      A      3.5           4.0
#2      B      4.0           4.0

The library plyr is very helpful for stuff like this

library(plyr)
new.df <- ddply(df, c("method", "sp"), summarise,
                mean.length=mean(length),
                max.length=max(length),
                n.obs=length(length))

gives you

> new.df
  method sp mean.length max.length n.obs
1      A  1           4          6     2
2      A  2           3          4     2
3      B  1           6          8     2
4      B  2           2          3     2

More examples at http://www.inside-r.org/packages/cran/plyr/docs/ddply.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!