Enrique Enrique - 3 months ago 10
R Question

Loop over multiple data frames

I've got 4 data frame with this structure:

data1:

1.8064 2.2016 2.4506 2.1828 2.1171 1.9308 2.1707 2.1885
2.2310 2.2400 1.9115 2.1527 2.0934 1.7989 2.2144 2.0091
1.9248 2.2038 1.9676 1.9224 1.9502 1.7990 2.0824 2.1300
2.0095 2.0341 1.8433 1.8361 1.9958 1.8243 2.0397 2.0482
2.1143 2.2627 1.7620 1.7561 1.9490 1.9803 1.9336 2.2511
2.2377 2.5414 1.7867 1.6618 2.5090 1.8325 2.0212 2.1616
2.3476 2.1878 2.0469 1.7508 2.2969 1.7939 2.0291 2.0721
2.3534 2.0932 2.3502 1.9960 2.0710 1.9923 1.7787 1.9772
2.2607 2.1504 2.3685 2.1148 2.1961 1.7738 1.8405 2.0135
2.2411 1.9916 2.4726 2.0347 2.0751 1.7570 1.8874 1.9385


data2:

2.1913 1.8981 2.2441 2.3068 2.1198 2.1484 1.8056 1.7747
2.0842 1.8750 2.3023 2.1204 1.8972 2.1534 1.8028 1.9401
2.2105 1.9618 2.2472 1.9656 2.3098 1.9771 1.9520 1.8627
2.2863 1.9959 2.1781 1.9544 1.9281 1.9286 1.9699 2.0330
2.1987 2.0583 2.0953 2.0206 2.1148 2.3789 1.7052 1.9145
2.0513 2.0850 1.9810 2.4943 1.9120 2.2209 1.9461 2.0882
2.0049 2.0416 1.9303 2.3681 1.8974 2.0054 1.9261 1.9097
1.6882 2.1196 1.8641 2.3600 2.0931 1.7641 2.1131 1.7748
1.8840 1.7604 1.7664 2.2000 2.0055 1.8229 1.9871 1.9168
1.7340 1.9656 1.8480 2.0523 1.9950 1.8716 1.9206 1.7786
1.9604 1.9804 1.9601 2.0599 1.8969 1.8087 2.1845 1.8598


data3:

1.8064 2.2016 2.4506 2.1828 2.1171 1.9308 2.1707 2.1885
2.2310 2.2400 1.9115 2.1527 2.0934 1.7989 2.2144 2.0091
1.9248 2.2038 1.9676 1.9224 1.9502 1.7990 2.0824 2.1300
2.0095 2.0341 1.8433 1.8361 1.9958 1.8243 2.0397 2.0482
2.1143 2.2627 1.7620 1.7561 1.9490 1.9803 1.9336 2.2511
2.2377 2.5414 1.7867 1.6618 2.5090 1.8325 2.0212 2.1616
2.3476 2.1878 2.0469 1.7508 2.2969 1.7939 2.0291 2.0721
2.3534 2.0932 2.3502 1.9960 2.0710 1.9923 1.7787 1.9772
2.2607 2.1504 2.3685 2.1148 2.1961 1.7738 1.8405 2.0135
2.2411 1.9916 2.4726 2.0347 2.0751 1.7570 1.8874 1.9385


data4:

2.1913 1.8981 2.2441 2.3068 2.1198 2.1484 1.8056 1.7747
2.0842 1.8750 2.3023 2.1204 1.8972 2.1534 1.8028 1.9401
2.2105 1.9618 2.2472 1.9656 2.3098 1.9771 1.9520 1.8627
2.2863 1.9959 2.1781 1.9544 1.9281 1.9286 1.9699 2.0330
2.1987 2.0583 2.0953 2.0206 2.1148 2.3789 1.7052 1.9145
2.0513 2.0850 1.9810 2.4943 1.9120 2.2209 1.9461 2.0882
2.0049 2.0416 1.9303 2.3681 1.8974 2.0054 1.9261 1.9097
1.6882 2.1196 1.8641 2.3600 2.0931 1.7641 2.1131 1.7748
1.8840 1.7604 1.7664 2.2000 2.0055 1.8229 1.9871 1.9168
1.7340 1.9656 1.8480 2.0523 1.9950 1.8716 1.9206 1.7786
1.9604 1.9804 1.9601 2.0599 1.8969 1.8087 2.1845 1.8598


I need to get column 1 from data1, column 1 from data2, column 1 from data3 and column 1 from data4 and combine them into a single data frame, side by side and do the same with the other columns.

I was using this method, but it is a little bit rudimentary

dat1 <- data.frame(data1$V1)
dat2 <- data.frame(data2$V1)
dat3 <- data.frame(data3$V1)
dat4 <- data.frame(data4$V1)

final_data1 <- cbind(dat1,dat2,dat3,dat4)
.
.
.


dat1 <- data.frame(data1$V8)
dat2 <- data.frame(data2$V8)
dat3 <- data.frame(data3$V8)
dat4 <- data.frame(data4$V8)

final_data8 <- cbind(dat1,dat2,dat3,dat4)


Is there any way to do this with a loop?

Answer

We can loop through columns, bind them, and keep the resulting 8 dataframes in a list:

res <- lapply(1:8, function(i){ cbind(data1[i], data2[i], data3[i], data4[i]) })