Hi, You may also try ?reshape() dat2<- dat1 names(dat2)[-c(1:21)]<- paste(rep(names(dat2)[1:21],8),rep(2:9,each=21),sep="_") names(dat2)[1:21]<- paste(names(dat2)[1:21],rep(1,21),sep="_") res1<- reshape(dat2,direction="long",varying=1:ncol(dat2),sep="_") row.names(res1)<- 1:nrow(res1) attr(res1,"reshapeLong")<-NULL
### res<-do.call(rbind,lapply(split(colnames(dat1),((seq_len(ncol(dat1))-1)%/%21)+1),function(x) {x1<- dat1[,x]; colnames(x1)<- paste0("V",1:21);x1}))##previous solution changed "paste" to "paste0" row.names(res)<- 1:nrow(res) all.equal(res,res2) #[1] TRUE A.K. ----- Original Message ----- From: arun <smartpink...@yahoo.com> To: "w...@dataanalyticscorp.com" <w...@dataanalyticscorp.com> Cc: R help <r-help@r-project.org> Sent: Sunday, September 8, 2013 9:29 AM Subject: Re: [R] melting a data frame 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. <w...@dataanalyticscorp.com> To: R help <r-help@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 w...@dataanalyticscorp.com www.dataanalyticscorp.com ______________________________________________ R-help@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@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.