I have the 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 the following to get a count of the number of samples of each species by method:
n <- with(df,tapply(sp,method,function(x) count(x)))
How do I also get the mean length by method for each species?
The library plyr is very helpful for stuff like this
gives you
More examples at http://www.inside-r.org/packages/cran/plyr/docs/ddply.
Personally I would use
aggregate
:for everything together you may want: