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.