[R] Simple question about data.frame reduction

Gabor Grothendieck ggrothendieck at gmail.com
Wed Sep 2 01:47:07 CEST 2009


Try this:

> with(unique(d), tapply(var1, var3, sum))
a1 b1 c1 d1
 2  1  1  1


2009/9/1 Ronaldo Reis Júnior <chrysopa at gmail.com>:
> Hi,
>
> this is a simple question
>
> I have this data.frame:
>
>> test <-
> data.frame(var1=c(1,1,1,1,1,1),var2=c("a","a","b","c","d","e"),var3=c("a1","a1","b1","a1","c1","d1"))
>> test
>  var1 var2 var3
> 1    1    a   a1
> 2    1    a   a1
> 3    1    b   b1
> 4    1    c   a1
> 5    1    d   c1
> 6    1    e   d1
>
> Then I need to calculate the number of var3 excluding the repeated measure of
> var2.
>
> With tapply I have this:
>
>> tapply(test$var1,test$var3,sum)
> a1 b1 c1 d1
>  3  1  1  1
>
> But the correct result is:
>
> a1 b1 c1 d1
>  2  1  1  1
>
> because on factor "a1" in var3 I have 2 repeated factor "a" in var2.
>
> I try a simple solution for it but without success. aggregate function dont
> work with factors (var3 by var1).
>
> Anybody have an idea?
>
> Thanks
> Ronaldo
> --
> "It's men like him that give the Y chromosome a bad name."
> --
>> Prof. Ronaldo Reis Júnior
> |  .''`. UNIMONTES/DBG/Lab. Ecologia Comportamental e Computacional
> | : :'  : Campus Universitário Prof. Darcy Ribeiro, Vila Mauricéia
> | `. `'` CP: 126, CEP: 39401-089, Montes Claros - MG - Brasil
> |   `- Fone: (38) 3229-8192 | ronaldo.reis at unimontes.br | chrysopa at gmail.com
> | http://www.ppgcb.unimontes.br/lecc | ICQ#: 5692561 | LinuxUser#: 205366
> --
> Favor NÃO ENVIAR arquivos do Word ou Powerpoint
> Prefira enviar em PDF, Texto, OpenOffice (ODF), HTML, or RTF.
>
> ______________________________________________
> 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