简体   繁体   中英

Interleaving two data.frames of data.frames in R

I have two lists of dataframes in R. I want each dataframe from the two sets to interleave with one another, such that the first dataframe from the first list interleaves with the first dataframe of the second list.

They all have the same column names. They look like this:

  ind_1 ind_2 ind_3 ind_4 ind_5 ind_6 ind_7 ind_8 ind_9 ind_10 ind_11 ind_12 ind_13 ind_14 ind_15 ind_16
1     0     0     0     0     0     0     0     0     1      1      0      0      1      0      1      0
2     0     0     0     0     0     0     0     0     1      1      0      0      1      1      0      0
3     0     0     0     0     0     0     0     0     1      1      0      0      0      1      1      0
4    32    30    35    26    31    35    15    28    26     16     23     32     21      0     25     35
5    32    30    35    26    31     0    15    28    26     16     23     32     21     27     25     35
6    32    30    35    26    31    35    15    28    26     16     23     32     21     27     25     35
7    32    30    35    26    31    35    15    28    26     16     23     32     21     27     25     35
8    32    30    35    26    31    35    15    28    26     16     23     32     21     27     25     35
9    32    30    35    26    31    35    15    28    26     16     23     32     21     27     25     35

All of the files are fairly similar, so if testing this you could use this file over and over. The goal would be:

  ind_1 ind_2 ind_3 ind_4 ind_5 ind_6 ind_7 ind_8 ind_9 ind_10 ind_11 ind_12 ind_13 ind_14 ind_15 ind_16
1     0     0     0     0     0     0     0     0     1      1      0      0      1      0      1      0
1     0     0     0     0     0     0     0     0     1      1      0      0      1      0      1      0
2     0     0     0     0     0     0     0     0     1      1      0      0      1      1      0      0
2     0     0     0     0     0     0     0     0     1      1      0      0      1      1      0      0
3     0     0     0     0     0     0     0     0     1      1      0      0      0      1      1      0
3     0     0     0     0     0     0     0     0     1      1      0      0      0      1      1      0
4    32    30    35    26    31    35    15    28    26     16     23     32     21      0     25     35
4    32    30    35    26    31    35    15    28    26     16     23     32     21      0     25     35
5    32    30    35    26    31     0    15    28    26     16     23     32     21     27     25     35
5    32    30    35    26    31     0    15    28    26     16     23     32     21     27     25     35

I have left the R index row numbers there because it keeps the columns straight in this visualization, but the do not matter (the 1,1 2,2 etc on the far left).

It works just fine with one data.frame, but not applied to a list of data frames. This is what I have tried:

interleaved <- lapply(new_list, function(y) 
{y<-interleave(y,second_list)})

where one list of data frames is named new_list and the other second_list. I have also tried:

interleaved <- lapply(new_list, second_list, function(x,y) 
{y<-interleave(x,y)})

If anyone knows how to solve this problem it would be very much appreciated.

We need to use Map for interleave ing corresponding elements of both list s

library(gdata)
out_lst <- Map(interleave, new_list, second_list)

Or another option is map2 from purrr

library(purrr)
out_lst <- map2(new_list, second_list, interleave)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM