Transforming results of the summary function

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Transforming results of the summary function

Maciej Bliziński-3
Hi all,

I have a question about transforming the data from summary function.
Let's say I have a data frame like this:

> x = data.frame(a = c(rep("lev1", 5), rep("lev2", 5)), b = c(rnorm(5)+2, rnorm(5)))
> x
      a          b
1  lev1  1.5964765
2  lev1  2.2945609
3  lev1  3.5285787
4  lev1  1.4439838
5  lev1  2.2948826
6  lev2  1.7063506
7  lev2 -0.4042742
8  lev2 -1.6485337
9  lev2 -1.1163817
10 lev2 -0.2023246

I'd like to create quantiles for every level of the "a" column. Looking
into the documentation, I found the function "by" with which I can
perform the calculations...

> xs = by(x, x$a, function(x) summary(x$b))
> xs
x$a: lev1
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  1.444   1.596   2.295   2.232   2.295   3.529
------------------------------------------------------------
x$a: lev2
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-1.6490 -1.1160 -0.4043 -0.3330 -0.2023  1.7060

...but I need them saved as a CSV file, so I'd like the result to have the form of:

level    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
 lev1   1.444   1.596   2.295   2.232   2.295   3.529
 lev2  -1.6490 -1.1160 -0.4043 -0.3330 -0.2023  1.7060

Can you give me any hints?

Regards,
Maciej

--
Maciej Bliziński <[hidden email]>
http://automaciej.blogspot.com/

______________________________________________
[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
Reply | Threaded
Open this post in threaded view
|

Re: Transforming results of the summary function

Gabor Grothendieck
Try this:

do.call("rbind", xs)


On 2/17/06, Maciej Bliziński <[hidden email]> wrote:

> Hi all,
>
> I have a question about transforming the data from summary function.
> Let's say I have a data frame like this:
>
> > x = data.frame(a = c(rep("lev1", 5), rep("lev2", 5)), b = c(rnorm(5)+2, rnorm(5)))
> > x
>      a          b
> 1  lev1  1.5964765
> 2  lev1  2.2945609
> 3  lev1  3.5285787
> 4  lev1  1.4439838
> 5  lev1  2.2948826
> 6  lev2  1.7063506
> 7  lev2 -0.4042742
> 8  lev2 -1.6485337
> 9  lev2 -1.1163817
> 10 lev2 -0.2023246
>
> I'd like to create quantiles for every level of the "a" column. Looking
> into the documentation, I found the function "by" with which I can
> perform the calculations...
>
> > xs = by(x, x$a, function(x) summary(x$b))
> > xs
> x$a: lev1
>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
>  1.444   1.596   2.295   2.232   2.295   3.529
> ------------------------------------------------------------
> x$a: lev2
>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
> -1.6490 -1.1160 -0.4043 -0.3330 -0.2023  1.7060
>
> ...but I need them saved as a CSV file, so I'd like the result to have the form of:
>
> level    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
>  lev1   1.444   1.596   2.295   2.232   2.295   3.529
>  lev2  -1.6490 -1.1160 -0.4043 -0.3330 -0.2023  1.7060
>
> Can you give me any hints?
>
> Regards,
> Maciej
>
> --
> Maciej Bliziński <[hidden email]>
> http://automaciej.blogspot.com/
>
> ______________________________________________
> [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
>

______________________________________________
[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