[R] Alternative and more efficient data manipulation

Sam Albers tonightsthenight at gmail.com
Tue Aug 16 01:57:34 CEST 2011


Hello list,

## I have been doing the following process to convert data from one
form to another for a while but it occurs to me that there is probably
an easier way to do this. I am often given data that have column names
which are actually data and I much prefer dealing with data that are
sorted by factors. So to convert the columns I have previously made
use of make.groups() in the lattice package which works completely
satisfactorily. However, it is a bit clunky for what I am using it for
and I have to carry the other variables forward. Can anyone suggest a
better way of converting data like this?

library(lattice)

dat <- data.frame(`x1`=runif(6, 0, 125),
                  `x2`=runif(6, 50, 75),
                  `x3`=runif(6, 0, 100),
                  `x4`=runif(6, 0, 200),
                  date =
as.Date(c("2009-09-25","2009-09-28","2009-10-02","2009-10-07","2009-10-15","2009-10-21")),
                  yy= head(letters,2), check.names=FALSE)
## Here is an example of the type of data that NEED converting
dat

dat.group <- with(dat, make.groups(x1,x2,x3,x4))
## Carrying the other variables forward
dat.group$date <- dat$date
dat.group$yy <- dat$yy
## Here is an example of what I would like the data to look like
dat.group

## The point of this all is so that I can used the data in a manner
such as this:
with(dat.group, xyplot(data ~ as.numeric(substr(which, 2,2))|yy, groups=date))

## So I suppose what I am asking is if there is a more efficient way
of doing this?

Thanks so much in advance!

Sam



More information about the R-help mailing list