Home > database >  Loop through list of data frames and take every n columns to create new data frame
Loop through list of data frames and take every n columns to create new data frame

Time:02-05

I currently have a list of data frames and want to loop through each list to grab every ten columns and put them together in a new data frame. To clarify, I want to go through the first data frame, grab 10 columns, make those columns 1:10 in the new data frame, then go to the next, grab 10 columns, and make them 11:20 in the new data frame. I want to keep doing this process until I go through each list of data frames and complete the list.

I've tried the below:

a1<-rnorm(100)
a2<-rnorm(100)
b1<-rnorm(100)
b2<-rnorm(100)
c1<-rnorm(100)
c2<-rnorm(100)

dataframe_list <- vector("list", 5)
for (i in seq_along(dataframe_list)) {
  dataframe_list[[i]] <- rbind(a1,a2,b1,b2,c1,c2)
}

new_df_list <- list()
j = 1:10
while (max(j) < 101) {
  for (i in seq_along(dataframe_list)) {
    new_df[,j] <- dataframe_list[[i]][,j]
    new_df_list[[i]]<- new_df
  }
  j = j   10
}

But I think this is just replacing the ten columns with the next ten in the list.

Thanks in advance.

CodePudding user response:

We could use map function from purrr package for this. Afterwards we use bind_cols from dplyr package:

library(purrr)
library(dplyr)

my_df <- map(dataframe_list,~(.x %>% as_tibble() %>% 
                       select(1:10))) %>% 
  bind_cols() %>% 
  rename_with(., ~paste0("col", 1:100))
# A tibble: 6 × 50
    col1   col2   col3   col4   col5    col6   col7    col8   col9  col10  col11  col12  col13  col14  col15   col16
   <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>  <dbl>   <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>
1 -1.21   1.27   0.702  1.32  -1.14  -0.194  -0.706  0.916   0.450  0.767 -1.21   1.27   0.702  1.32  -1.14  -0.194 
2 -0.473  1.67   1.26  -0.177 -0.757 -0.308  -0.678 -0.844   0.705  0.301 -0.473  1.67   1.26  -0.177 -0.757 -0.308 
3  0.443  2.02  -2.62   1.24  -1.13  -0.312  -0.190  1.05   -0.552  1.53   0.443  2.02  -2.62   1.24  -1.13  -0.312 
4  1.28  -0.525 -1.09  -1.64   0.326 -0.570   0.986 -0.620   0.616 -0.168  1.28  -0.525 -1.09  -1.64   0.326 -0.570 
5  0.609 -0.102 -2.40  -1.32   0.818 -0.0271 -0.804  0.0921  0.759  0.211  0.609 -0.102 -2.40  -1.32   0.818 -0.0271
6  0.274 -0.830  0.381  0.343  0.330  0.278   0.726  1.67   -0.318  1.26   0.274 -0.830  0.381  0.343  0.330  0.278 
# … with 34 more variables: col17 <dbl>, col18 <dbl>, col19 <dbl>, col20 <dbl>, col21 <dbl>, col22 <dbl>, col23 <dbl>,
#   col24 <dbl>, col25 <dbl>, col26 <dbl>, col27 <dbl>, col28 <dbl>, col29 <dbl>, col30 <dbl>, col31 <dbl>,
#   col32 <dbl>, col33 <dbl>, col34 <dbl>, col35 <dbl>, col36 <dbl>, col37 <dbl>, col38 <dbl>, col39 <dbl>,
#   col40 <dbl>, col41 <dbl>, col42 <dbl>, col43 <dbl>, col44 <dbl>, col45 <dbl>, col46 <dbl>, col47 <dbl>,
#   col48 <dbl>, col49 <dbl>, col50 <dbl>
# ℹ Use `colnames()` to see all variable names

CodePudding user response:

An option could be by first using lapply to select the first ten columns of each dataframe in your list saved as a list. After that, you can use bind_cols from dplyr to combine the dataframes into one dataframe. Here is some reproducible code:

library(dplyr)
set.seed(7) # reproducibility
df1 <- data.frame(replicate(20,sample(0:1,5,rep=TRUE)))
df2 <- data.frame(replicate(20,sample(0:1,5,rep=TRUE)))
df3 <- data.frame(replicate(20,sample(0:1,5,rep=TRUE)))
l <- list(df1, df2, df3)

df_filtered <- lapply(l, \(x) x[,1:10])
df_combined <- bind_cols(df_filtered)

df_combined  
#>   X1...1 X2...2 X3...3 X4...4 X5...5 X6...6 X7...7 X8...8 X9...9 X10...10
#> 1      1      1      1      0      0      0      0      0      0        1
#> 2      0      0      0      1      1      1      1      1      1        1
#> 3      0      1      1      0      1      1      0      1      1        0
#> 4      1      1      1      1      1      1      1      1      0        0
#> 5      0      1      1      1      0      0      1      1      0        0
#>   X1...11 X2...12 X3...13 X4...14 X5...15 X6...16 X7...17 X8...18 X9...19
#> 1       0       1       0       1       0       1       0       1       1
#> 2       0       1       1       0       1       0       1       1       1
#> 3       1       1       0       0       1       1       1       0       0
#> 4       1       0       1       0       1       0       1       1       1
#> 5       1       0       0       1       0       0       0       1       0
#>   X10...20 X1...21 X2...22 X3...23 X4...24 X5...25 X6...26 X7...27 X8...28
#> 1        1       1       1       1       0       0       0       0       1
#> 2        0       0       0       0       1       0       1       1       0
#> 3        0       1       0       1       0       0       1       0       0
#> 4        1       0       1       1       1       1       1       1       1
#> 5        0       0       1       0       1       0       1       0       1
#>   X9...29 X10...30
#> 1       0        1
#> 2       1        0
#> 3       0        0
#> 4       0        0
#> 5       1        1

Created on 2023-02-04 with reprex v2.0.2

CodePudding user response:

Using base R

setNames(do.call(rbind.data.frame, lapply(dataframe_list, head, 10)), 
   paste0("col", 1:100))

-output

    col1       col2       col3        col4       col5       col6       col7        col8       col9      col10       col11
a1  0.7793030  0.8841912  0.7256730 -0.08524323 -1.5596393 -0.7890424  1.5697219 -0.33654621 -0.3918948 -1.2193419 -0.03365252
a2 -1.5690657 -0.6538868 -0.7165864  1.81906352  0.2562943 -0.7977065  0.3269243  1.39661425 -1.1106302 -0.2516798 -0.08253067
b1 -0.6683287  1.2149600 -0.7072091  1.52961386 -1.4461004 -1.2166032 -0.6197978  0.14605286 -0.7379117  0.4066479  1.43679626
b2 -0.8429860  1.8727278 -0.3365289  1.06346899 -0.1593293 -1.0063262  0.7495746  0.10276405  1.0159325  0.8899335 -0.49866402
c1 -1.2564704  0.8536332  1.0737972 -1.38818780 -0.7225646 -0.8556486  2.7553238 -0.71280895  0.8959037 -1.4323856 -1.20640743
c2  0.6077386  0.5187255 -0.3555744 -0.07142831 -0.6076046 -0.2820873 -0.7573153 -0.04613757 -0.7928139  0.8464911  0.28639421
        col12      col13      col14       col15      col16       col17       col18     col19      col20      col21      col22
a1  0.5120785  0.6062648  0.7347130 -0.64246070 -1.7933904  0.11657383  1.96598080 1.4335286  0.2800078 -1.7016504  0.2535456
a2  0.4469310 -0.4348510  0.6337614  1.71715804 -0.2211258  0.61107952  0.05411653 0.4805574 -2.3078178 -0.2359667 -0.3897207
b1 -0.8733858 -1.2229974 -1.9789484 -0.06424681  2.2867944  0.08829771  0.69457271 0.6012572 -0.0406423  1.1822902 -0.6147050
b2 -0.8920748  0.5908152 -2.8652760  0.13183740 -0.9894885  0.28331050 -0.01529145 0.9484549  1.8160632 -0.5762984  0.1633331
c1  0.3514350 -0.6183382  0.9893045  1.19786700  0.3534182  1.41276696  0.22865727 0.4086079  0.8739275 -0.2749598  1.4794709
c2  0.1105736 -0.7947728 -0.1775345 -0.77035363 -1.1157140 -0.92719964 -0.74202720 1.6377775  0.6354976  0.6321249  1.6836422
        col23      col24      col25      col26      col27      col28      col29      col30      col31       col32      col33
a1  1.0240128  0.9835438  1.3677794  0.3082867 -1.0201232  0.3524333  0.6861180  1.8573144 -0.2299341 -0.72236322  0.8495473
a2 -0.5814792 -0.3148247  0.7141669  0.4010274 -0.5416061 -0.3104302  0.4687586 -0.3554200  0.9837601 -0.09010772 -0.5555971
b1 -1.2437221  0.7634912 -1.7936421 -1.6906611  0.3346778  0.4209662 -0.2519508 -2.7232369  0.9999341 -0.70299050 -0.0815552
b2 -0.4736396  0.6648603  0.5343397 -0.4054694 -0.3835265  0.8999484  1.1167929  1.4498313 -0.3473225  1.35935249  0.3236380
c1 -1.9960265  0.0776958 -1.6706888  1.1083987  0.1922520  0.3470949  2.3068525  1.6248107 -0.2760143  0.02223134 -2.0775715
c2 -0.0622619 -1.1820780  0.7475911 -1.3150508 -0.5920544 -1.2691909  0.7650461  0.3850958  0.5682972 -0.23255386 -0.3600243
        col34       col35      col36      col37      col38       col39       col40      col41      col42       col43      col44
a1 -1.0747343 -0.64127056  0.8406057 -0.4190465 -0.5190223 -0.62741844  0.09140228  0.1786809  1.0984214 -0.04853677  1.0179664
a2  0.6006356  0.74402923  0.3310838  1.8294160  1.5150618 -0.24280953  1.40826323 -0.9409830 -0.3820763 -0.03339920 -1.1755142
b1 -0.8873179  0.90903430 -0.9687017  0.7019327 -2.2772416  1.52807461 -0.54425360  0.3429737 -0.7505054  0.87557955 -1.1314788
b2  0.2031488 -0.93172680  0.5154006  0.2351499  1.3748313  1.07133405 -0.20361958  0.1362986  0.7740150 -0.85283613  0.9490901
c1  0.6894595 -0.02527927 -0.7486883  0.4070326  1.0156088  0.08941385  0.67335643 -0.3762824 -0.9780800  1.58938237 -0.8515895
c2  0.0325183  1.36884157 -0.4717803  0.5702121  0.8678536 -0.38389090  0.88268795  0.7532073  0.8429715 -0.70129253  0.4116780
        col45      col46     col47       col48       col49      col50      col51       col52      col53      col54      col55
a1 -0.4750872  0.9090170 0.6962839  0.09606974  1.00928085 -0.6617571 -1.7900352 -0.99124732  0.0717143  0.4148560 -1.1094484
a2 -1.0049608 -1.9616289 0.4113797 -0.49320198 -1.48715169 -0.7788449  1.2420158  1.34560478 -0.3127484 -1.5625836  0.9082922
b1 -0.2188074  0.2900230 1.5989995  0.19797974  2.00511740 -0.6318765 -0.6236234  0.68407922  0.7795980 -2.2538488  0.3939866
b2 -0.9787701 -0.3719629 0.2192201  0.90962619  0.02344414 -1.2228794  0.8293247  0.53628789 -0.8330837 -1.1463084  1.3088559
c1 -0.2965836  1.9758497 0.6588472 -1.94731886  0.10122057 -0.5536944 -0.1089527  0.08571981 -2.3571389  0.4387805 -0.4665783
c2 -1.1574806 -1.4127067 1.0171311 -1.71410232  0.16672603 -1.0914208  1.2950789  0.21185310 -0.3298243 -0.2845049  0.7687018
         col56      col57      col58      col59      col60      col61      col62         col63      col64      col65      col66
a1  0.01272115 -1.9844373  0.8520790  0.3279583 -0.2973611 -1.5521428  0.7643032  0.3921617392  0.9975850 -0.5645073  0.2214905
a2  0.96750744  1.1573404  0.6708822 -0.6946397  1.2315285  0.9887806  1.1335573  0.0001820987 -1.1563536  1.1548800  1.2597795
b1 -0.10738226 -0.6717304 -1.0758664 -0.5596440  0.8081687  2.3387244 -0.3766570  0.0812219759 -0.9799805 -3.2497680 -0.3252068
b2 -0.57600457  1.6004682 -1.4968619 -0.6195628 -0.6603415 -0.2201152  0.9212431 -0.6013577876 -0.9261861  1.5440999  0.4555838
c1  0.93322027 -0.6555662 -1.5485457 -0.3815470 -0.5487509 -0.1088095  1.1534634 -0.6924598008  0.8439085 -0.8973990 -0.1131937
c2 -0.96779003  0.5922429 -0.8897607 -0.4686718 -0.4415431 -0.8195054 -0.1596652 -0.7574782476 -0.3527167  0.8046849  0.3996969
         col67      col68      col69       col70       col71      col72      col73      col74      col75      col76      col77
a1 -0.68269511 0.03893115 -0.6436089  0.07773332 -0.28587683 -0.3028721 -0.1789556 -0.1632689 -0.1294269 -1.2202864  0.2151611
a2  0.59346701 0.69919256 -0.1317558  0.45761080  0.02450006 -1.7228764  0.4853908 -2.0343850 -0.9498310 -1.1288737  1.9986529
b1 -0.09814586 0.34259238  1.0246251 -0.16067195  0.67969015 -0.9472902 -1.4628413 -0.6212548  0.8266192  0.8142735  1.0774274
b2 -0.05629432 1.06592461  0.6577438 -0.90820974 -0.62916847  0.7231349  0.9737884 -1.1389942 -0.1312306 -0.5720297 -0.2506232
c1  0.18813132 1.16102101 -0.6344364 -0.55548641 -0.21728376 -0.7594900  0.4166999 -0.4967771  0.3892739 -0.5434174 -1.2368652
c2  1.25342296 1.53746759 -1.2278828 -0.30934653  0.34064661 -1.9134106  0.2497647 -0.9949310  0.4075976 -0.2902088 -1.4894533
         col78       col79       col80      col81      col82      col83       col84      col85      col86      col87      col88
a1  0.05863123  0.29188503  1.83927119 -0.1062726  1.8072097  0.3741005 -1.23424170 -0.9692847 -0.6721848  1.6429834  0.6449804
a2 -0.27791924  0.34388559  0.02434897  0.4015362  1.6221964 -1.9400274 -0.17843231  0.5275579  0.3619368  1.1351464  0.1219594
b1  0.28265044  1.36831797  1.93560633 -1.8268550  0.8097726 -0.7856248  0.64148703  1.1686414  0.5482865 -0.5876068  0.3335761
b2  0.76897643  0.01052551 -1.30880502  0.7691328 -0.6175673  0.5842387  0.22371124 -0.2220520  0.1437221 -0.7462069 -0.3082803
c1  0.69765562  2.81249004 -0.69575956  0.6537419 -1.2000512 -0.1885172 -0.16253778 -0.4880369  1.1737648  2.5936935  1.3076305
c2  1.49474609 -0.14955373  1.39893339 -0.5869510  1.2872034 -3.0537851  0.01186699 -0.1217406  0.2246990 -0.3786971 -1.2225998
        col89      col90      col91      col92      col93      col94      col95      col96       col97      col98      col99
a1  0.2131518 -0.1706756  1.0407849 -0.4877381  1.9409925 -0.4862350 0.40368228 -1.0189452  1.60333870 -0.2465299  0.6331086
a2 -0.8078525 -1.5610299 -0.5374766 -1.3210438  1.5579983  1.6835180 0.66376940  0.2311369 -0.01016803  1.2000827  0.3989912
b1  0.4425652  0.6929049  0.3789561 -0.6110659 -0.2744571  1.2633267 0.70118432  0.3623799 -0.37786629 -0.6579411 -1.2802693
b2 -0.6773242 -0.2979941 -0.6329421 -0.7203474  1.8497844  0.1000457 0.67462673 -2.1607909 -0.25726335  1.1928355 -1.5531827
c1 -1.2816743 -0.8374141  2.1771791  1.1570421 -1.6065999 -0.6932305 0.09213165 -0.3763484 -0.52642540  0.9988159  1.9761539
c2 -0.5157842 -0.1329479 -1.7129097 -0.6319982 -0.2939219  0.6110354 1.53061870 -1.1742069 -0.28214885 -1.7897660 -0.7202226
       col100
a1 -0.1994077
a2 -0.6941029
b1  1.5086469
b2 -0.4975319
c1  0.7100122
c2  0.3809441
...
  • Related