[R] melting a data frame

John Fox jfox at mcmaster.ca
Sun Sep 8 16:06:47 CEST 2013


Dear Walt and A.K.,

One shouldn't reflexively avoid loops in R. In this case, it seems to me clearer to use a loop, and it's no less "efficient" (especially, I would guess, when one takes into account the time to figure out how to do the computation). I get

> system.time({
+ res<-do.call(rbind,lapply(split(colnames(dat1),((seq_len(ncol(dat1))-1)%/%21)+1),function(x) {x1<- dat1[,x]; colnames(x1)<- paste("V",1:21);x1}))
+ row.names(res)<- 1:nrow(res)
+ })
   user  system elapsed 
   0.02    0.00    0.02 
> dim(res)
[1] 1170   21


> system.time({
+ res2 <- as.data.frame(matrix(0, 1170, 21))
+ for (i in 1:9){
+     res2[((i - 1)*130 + 1):(i*130), ] <- dat1[, ((i - 1)*21 + 1):(i*21)]
+ }
+ })
   user  system elapsed 
   0.02    0.00    0.01 
> dim(res2)
[1] 1170   21

> all(res == res2)
[1] TRUE

Best,
 John

On Sun, 8 Sep 2013 06:29:42 -0700 (PDT)
 arun <smartpink111 at yahoo.com> wrote:
> 
> 
> Hi,
> 
> You could try:
> set.seed(48)
> dat1<- as.data.frame(matrix(sample(1:40,189*130,replace=TRUE),ncol=189))
> res<-do.call(rbind,lapply(split(colnames(dat1),((seq_len(ncol(dat1))-1)%/%21)+1),function(x) {x1<- dat1[,x]; colnames(x1)<- paste("V",1:21);x1}))
>  row.names(res)<- 1:nrow(res)
>  dim(res)
> #[1] 1170   21
> A.K.
> 
> 
> 
> ----- Original Message -----
> From: Data Analytics Corp. <walt at dataanalyticscorp.com>
> To: R help <r-help at r-project.org>
> Cc: 
> Sent: Saturday, September 7, 2013 11:33 PM
> Subject: [R] melting a data frame
> 
> Hi,
> 
> Suppose I have a data frame with 189 columns.  The columns are actually 9 blocks of 21 columns each, each block representing measures on each of 9 products.  There are 130 rows.  Suppose I extract the first block of 21 columns and make them into a separate data frame.  I then want to take the second block of 21 columns and rbind it to the first; then the third set of 21 and rbind it to the first two; etc.  The final data frame should have 1170 (= 9 * 130)  rows and 21 columns.  Is there an easy way to melt the blocks comparable to using the melt function in the plyr package (which is why I'm referring to what I want to do as "melting")?  It seems that there should be a simple way to do this.  I used a for loop which worked, but I want to see if there's a more efficient way.
> 
> Thanks,
> 
> Walt
> 
> ________________________
> 
> Walter R. Paczkowski, Ph.D.
> Data Analytics Corp.
> 44 Hamilton Lane
> Plainsboro, NJ 08536
> ________________________
> (V) 609-936-8999
> (F) 609-936-3733
> walt at dataanalyticscorp.com
> www.dataanalyticscorp.com
> 
> ______________________________________________
> 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.
> 
> 
> ______________________________________________
> 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.

------------------------------------------------
John Fox
McMaster University
Hamilton, Ontario, Canada
http://socserv.mcmaster.ca/jfox/



More information about the R-help mailing list