[R] Simple question about data.frame reduction

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


Hi,

I find a simple solution:

aggregate(test$var2,list(test$var3,test$var2),length)

  Group.1 Group.2 x
1      a1       a 2
2      b1       b 1
3      a1       c 1
4      c1       d 1
5      d1       e 1

Thanks

Ronaldo

Em Ter 01 Set 2009, Ronaldo Reis Júnior escreveu:
> Dear Jorge,
>
> I already try this solution. But I need to retrieve the information in
> function of var3 and not var2, but excluding repeated measure in var2.
>
> > tapply(test$var2,test$var3,length)
>
> a1 b1 c1 d1
>  3  1  1  1
>
> but in a1 I need the result=2 and not 3 because two elements in var2 are
> repeated
>
> Thanks
> Ronaldo
>
> Em Ter 01 Set 2009, Jorge Ivan Velez escreveu:
> > Dear Ronaldo,
> > You were almost there!  Here is a suggestion:
> >
> >  with(test, tapply(var3, var2, length))
> > # a b c d e
> > # 2 1 1 1 1
> >
> > HTH,
> > Jorge
> >
> >
> > 2009/9/1 Ronaldo Reis Júnior <>
> >
> > > 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.
> >
> > 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.
-- 
Por que tem gente que acorda os outros para perguntar se estavam
dormindo?
--
> 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