[R] Data Frame Manipulations

michael watson (IAH-C) michael.watson at bbsrc.ac.uk
Wed Dec 1 12:50:45 CET 2004


Hi

I have used merge() to merge two data frames, very much like performing
a SQL join.  Now I want to do a few different SQL-style things and I
wondered if there were functions to do it... 

Is there a "group by" style function?  For example if I merge() two data
frames and end up with multiple rows for each "id", and want to take the
average of the values of a particular column?  I know I can probably put
something together using merge() and by() and then munging the results
together myself, but is there something in R to perform this
automatically?

The second thing I'd like to do is like a cross-tab query; that is when
after a merge() I end up with multiple rows for a particular "id", and
want to cross-tab the data so that the multiple values become columns
and I end up with one row for each "id" again e.g.

ID	Val
1	5
1	10
2	15
2	20

Becomes

ID	Val1	Val2
1	5	10
2	15	20

Thanks in advance!

Mick




More information about the R-help mailing list