[R] what fun I need to put in this "tapply"

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Mon May 14 20:10:12 CEST 2007



Weiwei Shi said the following on 5/14/2007 11:04 AM:
> Hi,
> I happened to need generate the following
>> t1
>   V1 V2 count count2
> 1  1 11     2      3
> 2  1 12     2      2
> 3  2 11     1      3
> 4  3 13     3      1
> 5  3 11     3      3
> 6  3 12     3      2
> 
> from
>   V1 V2
> 1  1 11
> 2  1 12
> 3  2 11
> 4  3 13
> 5  3 11
> 6  3 12
> 
> I am wondering what function of funx I need to put into
> tapply(t1[,2], t1[,2] funx)
> 
> to get that?
> 
> If I use length, I probabaly need to do merge by myself. Is there a better way?
> 
> thanks.
> 
> 

I think ?ave would be more useful here:

x <- "  V1 V2
1  1 11
2  1 12
3  2 11
4  3 13
5  3 11
6  3 12"

y <- read.table(textConnection(x))
y$count <- ave(y[, 1], y[, 1], FUN = length)
y$count2 <- ave(y[, 2], y[, 2], FUN = length)

HTH,

--sundar



More information about the R-help mailing list