[R] Equivalent to a BY command in SAS

Dylan Beaudette dylan.beaudette at gmail.com
Mon Apr 14 05:41:22 CEST 2008


On Sun, Apr 13, 2008 at 7:36 PM, zerfetzen <zerfetzen at yahoo.com> wrote:
>
>  Hi,
>  I'm very new to R and absolutely love it.  Does anyone know how to use
>  something in R that functions like a BY command in SAS?
>
>  For example, let's say you have a variable x, and you want to see the mean.
>  Easy...
>
>  > mean(x)
>
>  But what if you want to see the mean of x conditional on another discrete
>  variable?  My best attempts so far are something like...

Using the built-in dataset 'CO2' :

# compute the mean 'conc' for every level of 'Plant'
tapply(CO2$conc, CO2$Plant, FUN=mean)

Qn1 Qn2 Qn3 Qc1 Qc3 Qc2 Mn3 Mn2 Mn1 Mc2 Mc3 Mc1
435 435 435 435 435 435 435 435 435 435 435 435

?tapply for details
?by for more details


Dylan

>
>  > mean(x, y_cat=1)
>
>  ...which of course doesn't work.  I have downloaded plenty of R user guides
>  that are very informative, but am not seeing much on detailed descriptives
>  or data manipulation (for my life, I can't figure out how to sort an
>  attached data frame, but that's another issue).  Thanks.
>  --
>  View this message in context: http://www.nabble.com/Equivalent-to-a-BY-command-in-SAS-tp16670452p16670452.html
>  Sent from the R help mailing list archive at Nabble.com.
>
>  ______________________________________________
>  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.
>



More information about the R-help mailing list