[R] Alternative and more efficient data manipulation

Mikhail Titov mlt at gmx.us
Tue Aug 16 02:11:23 CEST 2011


?reshape

You have your data in a wide format, but you want it in a long format.
reshape can convert it both ways.

Mikhail


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
> Behalf Of Sam Albers
> Sent: Monday, August 15, 2011 6:58 PM
> To: r-help at r-project.org
> Subject: [R] Alternative and more efficient data manipulation
> 
> 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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list