Hi dear listers Do you know how to made a mean by one or more categorical variables? I can do that quite easy on SAS by in R I just can't do it E.g. Some fishes Sex Lenght male 15 fema 20 fema 17 fema 19 male 18 So the idea is mean(Lenght) by sex, in order to have sex: Male mean=XX Sex: Fema mean=YY In this case is quite easy to do it by a loop, but I have a huge DB so is not an option. Any suggestion? Thanks in advance! -- Jorge Cornejo Donoso Universidad Austral de Chile Portales 73, Coyhaique,CP 5950000, Region de Ais?n, Chile Tel +56 67 244520 Fax +56 67 239377
Hello, Jorge! This might help:> fish.dfSex Length 1 male 15 2 fema 20 3 fema 17 4 fema 19 5 male 18> str(fish.df)'data.frame': 5 obs. of 2 variables: $ Sex : Factor w/ 2 levels "fema","male": 2 1 1 1 2 $ Length: int 15 20 17 19 18> tapply(fish.df$Length,fish.df$Sex,mean)fema male 18.66667 16.50000>Sincerely, Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: hodgess at gator.uhd.edu From: "Jorge Cornejo Donoso" <jfc10816 at ucsc.edu> Subject: [R] RV: By sentence Hi dear listers Do you know how to made a mean by one or more categorical variables? I can do that quite easy on SAS by in R I just can't do it E.g. Some fishes Sex Lenght male 15 fema 20 fema 17 fema 19 male 18 So the idea is mean(Lenght) by sex, in order to have sex: Male mean=XX Sex: Fema mean=YY In this case is quite easy to do it by a loop, but I have a huge DB so is not an option. Any suggestion? Thanks in advance! -- Jorge Cornejo Donoso Universidad Austral de Chile
Well, you want a mean of length BY sex, so look at by()... See ?by by(your.data.frame,sex,mean) Petr Jorge Cornejo Donoso napsal(a):> > Hi dear listers > > Do you know how to made a mean by one or more categorical variables? > I can do that quite easy on SAS by in R I just can't do it E.g. > Some fishes > Sex Lenght > male 15 > fema 20 > fema 17 > fema 19 > male 18 > > So the idea is mean(Lenght) by sex, in order to have sex: Male > mean=XX Sex: Fema mean=YY > In this case is quite easy to do it by a loop, but I have a huge DB > so is not an option. Any suggestion? > > Thanks in advance! > > -- > > Jorge Cornejo Donoso > > Universidad Austral de Chile > > Portales 73, Coyhaique,CP 5950000, Region de Ais?n, Chile Tel +56 67 244520 > Fax +56 67 239377 > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > >-- Petr Klasterecky Dept. of Probability and Statistics Charles University in Prague Czech Republic