[R] Combine columns having same column name from multiple data frames
arun
smartpink111 at yahoo.com
Sat Nov 23 17:30:52 CET 2013
Hi,
You may try:
lapply(seq_len(ncol(data1)),function(i) {x1 <- do.call(cbind,lapply(lapply(as.list(paste0("data",1:3)),get),`[`,i)); write.csv(x1,paste0("new",i,".csv"),quote=FALSE) })
A.K.
Dear All,
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.
Example:
> data1
1 2 3 4 5 6 7 8 9
x -39532.0 -39472.0 -39472.0 -39592.0 -39532.0 -39472.0 -39412.0 -39592.0 -39412.0
y 2015408.1 2015348.1 2015288.1 2015228.1 2015228.1 2015228.1 2015228.1 2015168.1 2015168.1
id 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
srad_120 496.0 496.0 496.0 496.0 496.0 496.0 496.0 496.0 496.0
srad_121 441.6 441.6 441.6 441.6 441.6 441.6 441.6 441.6 441.6
srad_122 150.4 150.4 150.4 150.4 150.4 150.4 150.4 150.4 150.4
srad_123 249.6 249.6 249.6 249.6 249.6 249.6 249.6 249.6 249.6
srad_124 272.0 272.0 272.0 272.0 272.0 272.0 272.0 272.0 272.0
srad_125 153.6 153.6 153.6 153.6 153.6 153.6 153.6 153.6 153.6
> data2
1 2 3 4 5 6 7 8 9
x -39532.0 -39472.0 -39472.0 -39592.0 -39532.0 -39472.0 -39412.0 -39592.0 -39412.0
y 2015408.1 2015348.1 2015288.1 2015228.1 2015228.1 2015228.1 2015228.1 2015168.1 2015168.1
id 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
srad_120 542.0 542.0 542.0 542.0 542.0 542.0 542.0 542.0 542.0
srad_121 487.6 487.6 487.6 487.6 487.6 487.6 487.6 487.6 487.6
srad_122 196.4 196.4 196.4 196.4 196.4 196.4 196.4 196.4 196.4
srad_123 295.6 295.6 295.6 295.6 295.6 295.6 295.6 295.6 295.6
srad_124 318.0 318.0 318.0 318.0 318.0 318.0 318.0 318.0 318.0
srad_125 199.6 199.6 199.6 199.6 199.6 199.6 199.6 199.6 199.6
> data3
1 2 3 4 5 6 7 8 9
x -39532.0 -39472.0 -39472.0 -39592.0 -39532.0 -39472.0 -39412.0 -39592.0 -39412.0
y 2015408.1 2015348.1 2015288.1 2015228.1 2015228.1 2015228.1 2015228.1 2015168.1 2015168.1
id 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
srad_120 578.0 578.0 578.0 578.0 578.0 578.0 578.0 578.0 578.0
srad_121 523.6 523.6 523.6 523.6 523.6 523.6 523.6 523.6 523.6
srad_122 232.4 232.4 232.4 232.4 232.4 232.4 232.4 232.4 232.4
srad_123 331.6 331.6 331.6 331.6 331.6 331.6 331.6 331.6 331.6
srad_124 354.0 354.0 354.0 354.0 354.0 354.0 354.0 354.0 354.0
srad_125 235.6 235.6 235.6 235.6 235.6 235.6 235.6 235.6 235.6
write.csv(cbind(data1[,1], data2[,1], data3[,1]), "new1.csv")
Above statement generates new csv file that has first column of each data frame (data1, data2, data3).
V1 V2 V3
x -39531.99524 -39531.99524 -39531.99524
y 2015408.131 2015408.131 2015408.131
id 1 1 1
srad_120 496 542 578
srad_121 441.6000061 487.6000061 523.6000061
srad_122 150.3999939 196.3999939 232.3999939
srad_123 249.6000061 295.6000061 331.6000061
srad_124 272 318 354
srad_125 153.6000061 199.6000061 235.6000061
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.
More information about the R-help
mailing list