[R] by or tapply?

jim holtman jholtman at gmail.com
Fri Jul 4 06:06:10 CEST 2008


Is this close to what you want:

> x
     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
> aggregate(x[,1:3], list(x[,4]), sum)
  Group.1 E1 E2 E3
1       C  0  1  0
2       E  0  0  0
3       N  0  1  3
4       U  0  0  1


On Thu, Jul 3, 2008 at 11:54 PM, Michael Denslow <mwdenslow at yahoo.com> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list