-1

I am trying to combine columns having same name from 3 different data frames and create new data frame with combined columns. The challenging thing is each data-frame has 100 columns so I have to create 100 new data frames and write each data-frame as new text file with unique column name.

enter image description here

write.csv(cbind(data1[,1], data2[,1], data3[,1]), "new1.csv")

this statement generates new csv file that has first column of each data frame (data1, data2, data3) as shown below.

enter image description here

Similarly I need to create 9 csv files using 9 columns in each data frame. If possible, please help in this.

Thank you very much.

upendra
  • 2,141
  • 9
  • 39
  • 64
  • 3
    Can you post some fake data and not a picture of data. R doesn't have an `read.image2data.frame` function, though now it may wind up in some package. – Tyler Rinker Nov 23 '13 at 17:46

1 Answers1

1

I guess this is a classic case of melting and reshaping

## list containing 3 data frames (10 obs, 5 columns)
df.list <- lapply(1:3, function(z) {
    m <- matrix(rnorm(50), nrow = 10)
    colnames(m) <- LETTERS[1:5]
    m
})
library(reshape2)
mdf <- melt(df.list)
list.by.var <- split(mdf, f = mdf1$Var2)
## list containing 5 data frames (10 obs, 3 columns)
lapply(list.by.var, dcast, formula = Var1 ~ L1)

OR in analogy to this post

bind.ith.cols <- function(i) do.call(cbind, lapply(df.list, "[", TRUE, i))
nc <- ncol(df.list[[1]])
lapply(1:nc, bind.ith.cols)
Community
  • 1
  • 1
adibender
  • 7,288
  • 3
  • 37
  • 41