[R] Sum by categorical variable

vito muggeo vito.muggeo at giustizia.it
Tue May 6 17:02:10 CEST 2003


If I understand correctly what you are looking for, see ?tapply.
Below there is a toy example

> NumVar<-runif(100) #continuous variable
> CategVar<-rbinom(100,1,.7) #unbalanced categorical variable
> table(CategVar)
CategVar
 0  1
33 67
> tapply(NumVar,CategVar,sum)
       0        1
15.17833 32.11759

sum can be replaced by any function including mean, median, and
user-defined.
best,
vito


----- Original Message -----
From: "De Amicis Fabrizio (G.I.T.)" <Fabrizio.DeAmicis at git.generali.ch>
To: "'r-help at lists.R-project.org'" <r-help at stat.math.ethz.ch>
Sent: Tuesday, May 06, 2003 4:08 PM
Subject: [R] Sum by categorical variable


> Dear R-list,
> I have two variables (numerical and categorical) and  would like to have
the
> sum (and maybe some other statistics) of the numerical variable by the
> categorical one.
> Can you help me,
> Thank you,
> Fabrizio
>
>
> ---------------------------------------------------------------
> Fabrizio De Amicis
>
> IT Department
> Generali Information Technologies - (GIT)
>
> Centro Galleria 2,
> Via Cantonale
> CH - 6928 Manno - Switzerland
> Tel  +41 91 806 6220
> Fax +41 91 806 6298
> E-mail: fabrizio.deamicis at git.generali.ch
>
>
>
>
> ************************************************************************
> The information in this email is confidential and may be legally...
{{dropped}}
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list