Re: [R] Grouping and Averaging in Table

From: Carlos Ortega <coforfe_at_gmail.com>
Date: Sat 18 Feb 2006 - 02:24:14 EST

Hello

Use something like this:

res.dat<-split(yourTable[,c(2,3)], group=yourTable$firstColum)

That will generate a list grouped by the firstColum. To get means, or whatever you need you will have to use any of the list functions "apply", "sapply", etc.

Regards,
Carlos.
P.S:
- yourTable: It is the name of your table or data.frame. - firstColum: It is the name of the first column of your table, data.frame.

On 2/17/06, Prasanna BALAPRAKASH <prasannaprakash@gmail.com> wrote:
>
> Dear Rs
>
> I have a single table with three columns in the following form:
>
> 1 100 150
> 1 45 32
> 1 99 100
> 2 150 33
> 2 22 87
> 2 71 31
> ....
> ....
> 1000 64 32
> 1 100 150
> 1 45 32
> 1 99 100
> 2 22 89
> 2 31 44
> 2 88 11
> ....
> ....
> 1200 64 32
> 1 100 150
> 1 45 32
> 1 99 100
> 2 150 33
> 2 22 87
> 2 71 31
> ...
> ...
> 1100 31 34
>
> Totally 1000+1200+1100 rows. Now, I need to group by first column
> and average then second and third column to get a table as follows as
> follows:
>
> 1 Avg. of all second col. values whose first col value is 1
> Avg. of
> all third col. values whose first col value is 1
> 2 Avg. of all second col. values whose first col value is 2
> Avg. of
> all third col. values whose first col value is 2
> ..
> ..
> 1200 Avg. of all second col. values whose first col value is 1200
> Avg. of all third col. values whose first col value is 1200
>
>
> Right now, I have a dirty implementation with a lot of "for" loops
> and "if" conditions. However, I am looking for some built in
> functions and lib. to make the code faster and easier.
>
>
> Thanks
> Prasanna
>
> ______________________________________________
> R-help@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
>

        [[alternative HTML version deleted]]



R-help@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 Received on Sat Feb 18 02:37:44 2006

This archive was generated by hypermail 2.1.8 : Mon 20 Feb 2006 - 14:08:40 EST