[R] Simple question about data.frame reduction

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Wed Sep 2 01:52:07 CEST 2009


You need to reverse the order of variable 2 and 3

tapply(test$var3,test$var2,length)

hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA  98504-5204


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> Behalf Of Ronaldo Reis Júnior
> Sent: Tuesday, September 01, 2009 4:43 PM
> To: R-Help
> Subject: Re: [R] Simple question about data.frame reduction
> 
> 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.
> --
> Pe�o paci�ncia aos impacientes que desejam meu desaparecimento: j�
> est� faltando menos que antes.
>                 -- Jacinto Benavente
> --
> > 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.
> 
> 	[[alternative HTML version deleted]]



More information about the R-help mailing list