2013-05-21 117 views
8

,我有以下數據:平均的變量由兩個因素

a <- c(1,1,1,1,2,2,2,2) 
b <- c(2,4,6,8,2,3,4,1) 
c <- factor(c("A","B","A","B","A","B","A","B")) 
df <- data.frame(
    sp=a, 
    length=b, 
    method=c) 

我可以用下面的方法來獲取每一個物種的樣本數的計數:

n <- with(df,tapply(sp,method,function(x) count(x))) 

如何我是否也通過每種物種的方法得到平均長度?

+1

順便說一句,爲了節省一些鍵入'with(df,tapply(sp,method,count))''在你的例子中可以正常工作。 –

回答

9

個人而言,我會用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 

一切在一起,你可能想:

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 
5

圖書館plyr是這樣的東西

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

非常有幫助的給你

> 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 

更多示例在http://www.inside-r.org/packages/cran/plyr/docs/ddply