[R] How to compute within-group mean and sd?

R. Michael Weylandt michael.weylandt at gmail.com
Sat Mar 24 16:37:14 CET 2012


This is a very commonly used paradigm in R and there are many
different answers, one would be tapply(). You could also look at the
plyr package.

Michael

On Sat, Mar 24, 2012 at 11:32 AM, reeyarn <reeyarn at gmail.com> wrote:
> Hi, I want to run something like
>  SELECT firm_id, count(*), mean(value), sd(value)
>  FROM table
>  GROUP BY firm_id;
>
> But I have to write a for loop like
>  for ( id in unique(table$firm_id ) {
>    print(paste(  id, mean(table[firm_id == id, "value"])  ))
>  }
>
> Is there any way to do it easier? Thanks :)
>
>
> Best,
> Reeyarn Lee
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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