Quantcast

How to compute within-group mean and sd?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to compute within-group mean and sd?

Reeyarn_李智洋_10928113
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to compute within-group mean and sd?

Michael Weylandt
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to compute within-group mean and sd?

Rui Barradas
In reply to this post by Reeyarn_李智洋_10928113
Hello,

Reeyarn_李智洋_10928113 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]]

______________________________________________
[hidden email] 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.
You can use sql with package 'sqldf'.

library(sqldf) # It needs package 'tcltk'
?sqldf

tbl <- data.frame(firm_id=rep(c(1,2), 10), value=rnorm(20))

sqldf("SELECT firm_id, count(*), avg(value), stdev(value)
  FROM tbl
  GROUP BY firm_id;")

I've changed the name of your data.frame because 'table' is an R function.

Hope this helps,

Rui Barradas
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to compute within-group mean and sd?

glsnow
In reply to this post by Reeyarn_李智洋_10928113
In addition to Michael's answers, there are packages that allow you to
use SQL syntax on R data objects, so you could probably just use what
you are familiar with.

On Sat, Mar 24, 2012 at 9:32 AM, reeyarn <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to compute within-group mean and sd?

Reeyarn_李智洋_10928113
In reply to this post by Rui Barradas
Thanks Rui, Greg, and Michael.
Your answers helped a lot!

Best,
Reeyarn

On Sun, Mar 25, 2012 at 1:02 AM, Rui Barradas <[hidden email]> wrote:

> Hello,
>
>
> You can use sql with package 'sqldf'.
>  library(sqldf) # It needs package 'tcltk'
> ?sqldf
>  tbl <- data.frame(firm_id=rep(c(1,2), 10), value=rnorm(20))
>  sqldf("SELECT firm_id, count(*), avg(value), stdev(value)
>  FROM tbl
>  GROUP BY firm_id;")
>
> I've changed the name of your data.frame because 'table' is an R function.
>  Hope this helps,
>
> Rui Barradas
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Loading...