Skip to content
Prev 148999 / 398498 Next

by or tapply?

Dear R-helpers,

I have a data frame that is similar to the one below.
The row names are species and the E1, E2, E3 columns are presence/absence for a given site. I have an additional column 'origin' which has information about the species.

     E1 E2 E3 origin
BASA  0  0  1      N
BASO  0  0  1      N
BRCO  0  0  1      U
CAER  0  0  0      E
CAGI  0  1  0      C
CEFL  0  1  1      N

I want to get the sum of ALL of the sites for the factors in the column 'origin'.

I have used 

by(merge[,2],merge$origin,sum)
and
tapply(merge[,2],merge$origin,sum)

but this just gives me one column at a time.
How can I get all of the sites summed individually and end up with the site names as rows and the factors be the columns.

Thanks in advance for your help!
Michael