[R] merge counts from table()
David L Carlson
dcarlson at tamu.edu
Mon Aug 13 21:01:04 CEST 2012
If you know all of the categories in advance, you can convert x and y to
factors and then sum the tables:
> x<-1:4
> y<-2:5
> x <- factor(x , levels=1:5) # list all possible categories
> y <- factor(y , levels=1:5)
> table(x)
x
1 2 3 4 5
1 1 1 1 0
> table(y)
y
1 2 3 4 5
0 1 1 1 1
> table(c(x, y))
1 2 3 4 5
1 2 2 2 1
> table(x) + table(y)
x
1 2 3 4 5
1 2 2 2 1
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Francois Pepin
> Sent: Monday, August 13, 2012 1:26 PM
> To: r-help at r-project.org
> Subject: [R] merge counts from table()
>
> Hi everyone,
>
> Is there an easy way to combine the counts from table()?
>
> Let's say that I have:
> x<-1:4
> y<-2:5
>
> I want to replicate:
> table(c(x,y))
>
> using only table(x) and table(y) as input.
>
> The reason is that it's cumbersome to carry all the values around when
> all I care about are the counts. The actual situation has about a
> billion counts over ~150 categories.
>
> I know there's got to be a number of ways of doing things (plyr comes
> to mind), but I can't seem to find how to accomplish it.
>
> Thanks,
>
> François Pepin
> ______________________________________________
> 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.
More information about the R-help
mailing list