[R] Grouping and Averaging in Table

Prasanna BALAPRAKASH prasannaprakash at gmail.com
Fri Feb 17 16:28:46 CET 2006


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




More information about the R-help mailing list