简体   繁体   English

在 R 中交错两个 data.frames 的 data.frames

[英]Interleaving two data.frames of data.frames in R

I have two lists of dataframes in R.我在 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.我希望这两组中的每个 dataframe 相互交错,以便第一个列表中的第一个 dataframe 与第二个列表的第一个 dataframe 交错。

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).我已将 R 索引行号留在那里,因为它使列在此可视化中保持直线,但没关系(最左边的 1,1 2,2 等)。

It works just fine with one data.frame, but not applied to a list of data frames.它适用于一个 data.frame,但不适用于数据帧列表。 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.其中一个数据帧列表命名为 new_list,另一个命名为 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我们需要使用Mapinterleave两个list的相应元素

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

Or another option is map2 from purrr或者另一个选项是来自purrrmap2

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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