r - Mean of variable by two factors -


i have following data:

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) 

i can use following count of number of samples of each species method:

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

how mean length method each species?

personally use aggregate:

aggregate(length ~ sp, data = df, fun= "mean" ) # species #     sp length #1  1    5.0 #2  2    2.5  aggregate(length ~ sp + method, data = df, fun= "mean" )     # species , method #  sp method length #1  1           4 #2  2           3 #3  1      b      6 #4  2      b      2 

for may want:

aggregate(length ~ method, data = df, function(x) c(m = mean(x), counts = length(x)) )  # counts , mean each method #  method length.m length.counts #1           3.5           4.0 #2      b      4.0           4.0 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -