[R] Simple question about data.frame reduction

Ronaldo Reis Júnior chrysopa at gmail.com
Wed Sep 2 01:22:44 CEST 2009


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.




More information about the R-help mailing list