[R] Data Frame Manipulations
dimitris.rizopoulos at med.kuleuven.ac.be
Wed Dec 1 13:13:58 CET 2004
regarding your second question you could use the `reshape()' function,
dat <- data.frame(ID=rep(1:2, each=2), Val=seq(5,20,5))
dat$time <- unlist(lapply(split(dat$ID, dat$ID), function(x)
reshape(dat, direction="wide", idvar="ID", v.names="Val")
I hope it helps.
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
----- Original Message -----
From: "michael watson (IAH-C)" <michael.watson at bbsrc.ac.uk>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, December 01, 2004 12:50 PM
Subject: [R] Data Frame Manipulations
> I have used merge() to merge two data frames, very much like
> 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
> frames and end up with multiple rows for each "id", and want to take
> average of the values of a particular column? I know I can probably
> something together using merge() and by() and then munging the
> together myself, but is there something in R to perform this
> The second thing I'd like to do is like a cross-tab query; that is
> after a merge() I end up with multiple rows for a particular "id",
> want to cross-tab the data so that the multiple values become
> and I end up with one row for each "id" again e.g.
> ID Val
> 1 5
> 1 10
> 2 15
> 2 20
> ID Val1 Val2
> 1 5 10
> 2 15 20
> Thanks in advance!
> R-help at stat.math.ethz.ch mailing list
> PLEASE do read the posting guide!
More information about the R-help