Hello,
My earlier solution has a problem. If you have billions of counts over
~150 categories, it will recreate the full vectors every time you add a
new category, thus causing potential memory issues. Another, much more
complicated, way but using only the tables info is as follows.
twotable <- function(x, y) UseMethod("twotable")
twotable.default <- function(x, y) twotable( table(x), table(y) )
twotable.table <- function(x, y){
if(class(y) != "table") y <- table(y)
x.nms <- names(x)
y.nms <- names(y)
nms <- unique(c(x.nms, y.nms))
result <- integer( length(nms) )
names(result) <- nms
result[ x.nms ] <- x
result[ y.nms ] <- result[ y.nms ] + y
dim(result) <- length(nms)
dimnames(result) <- list(x = nms)
class(result) <- "table"
result
}
twotable(x, y)
twotable(t1, y)
By using S3 it gets all information it needs from the objects that
already exist. It should also be faster (untested, just a feeling).
Rui Barradas
Em 13-08-2012 19:45, Rui Barradas escreveu:
Hello,
This one will do it.
x <- 1:4
y <- 2:5
t1 <- table(x)
t2 <- table(y)
(xy <- c(rep(names(t1), t1), rep(names(t2), t2)))
table(xy)
Hope this helps,
Rui Barradas
Em 13-08-2012 19:25, Francois Pepin escreveu:
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@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.
______________________________________________
R-help@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.
______________________________________________
R-help@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.