合并数据帧而不复制R中的行

合并数据帧而不复制R中的行,r,dataframe,R,Dataframe,我有一个庞大的数据帧列表,其中的数据结构使得合并变得困难。基本上,我读了很多嵌套目录,相当于.xls文件,因此我有一个数据帧列表,其中大约每5个数据帧应水平组合成行,每组5个数据帧应垂直组合 我将尝试给出一个示例(为了简单起见,组大小减少到3) df.list[1]: 身份证日期col1 col2 1日期1 2日期1 3日期1 df.list[2]: 身份证日期col3 col4 1日期1 2日期1 3日期1 df.list[3]: 身份证日期col5 col6 1日期1 2日期1 3日期1

我有一个庞大的数据帧列表,其中的数据结构使得合并变得困难。基本上,我读了很多嵌套目录,相当于.xls文件,因此我有一个数据帧列表,其中大约每5个数据帧应水平组合成行,每组5个数据帧应垂直组合

我将尝试给出一个示例(为了简单起见,组大小减少到3)

df.list[1]:
身份证日期col1 col2
1日期1
2日期1
3日期1
df.list[2]:
身份证日期col3 col4
1日期1
2日期1
3日期1
df.list[3]:
身份证日期col5 col6
1日期1
2日期1
3日期1
接下来的几个是不同的样本

df.list[4]:
id     Date     col1     col2
1      date2    <int>    <int>
2      date2    <int>    <int>
3      date2    <int>    <int>

df.list[5]:
id     Date     col3     col4
1      date2    <int>    <int>
2      date2    <int>    <int>
3      date2    <int>    <int>

df.list[6]:
id     Date     col5     col6
1      date2    <int>    <int>
2      date2    <int>    <int>
3      date2    <int>    <int>
df.list[4]:
身份证日期col1 col2
1日期2
2日期2
3日期2
df.list[5]:
身份证日期col3 col4
1日期2
2日期2
3日期2
df.list[6]:
身份证日期col5 col6
1日期2
2日期2
3日期2
这就持续了几千个样本。 我尝试使用dplyr中的bind_rows(),但结果是:

id     Date     col1     col2     col3     col4     col5     col6
1      date1    <int>    <int>      NA       NA       NA       NA
2      date1    <int>    <int>      NA       NA       NA       NA
3      date1    <int>    <int>      NA       NA       NA       NA
1      date1       NA       NA    <int>    <int>      NA       NA
2      date1       NA       NA    <int>    <int>      NA       NA
3      date1       NA       NA    <int>    <int>      NA       NA
1      date1       NA       NA      NA       NA     <int>    <int> 
2      date1       NA       NA      NA       NA     <int>    <int>
3      date1       NA       NA      NA       NA     <int>    <int>
1      date2    <int>    <int>      NA       NA       NA       NA
2      date2    <int>    <int>      NA       NA       NA       NA
3      date2    <int>    <int>      NA       NA       NA       NA
1      date2       NA       NA    <int>    <int>      NA       NA
2      date2       NA       NA    <int>    <int>      NA       NA
3      date2       NA       NA    <int>    <int>      NA       NA
1      date2       NA       NA      NA       NA     <int>    <int> 
2      date2       NA       NA      NA       NA     <int>    <int>
3      date2       NA       NA      NA       NA     <int>    <int>
id日期col1 col2 col3 col4 col5 col6
1日期1不,不,不
2日期1不,不,不
3日期1不,不,不
1日期1不,不,不
2日期1不,不,不
3日期1不,不,不
1日期1不,不,不
2日期1不,不,不
3日期1不,不,不
1日期2不,不,不
2日期2不,不,不
3日期2不,不,不
1日期2不,不,不
2日期2不,不,不
3日期2不,不,不
1日期2不,不,不
2日期2不,不,不
3日期2不,不,不
这并不是世界末日,但文件大小是它需要的5倍。理想情况下,数据的结构如下:

id     Date     col1     col2     col3     col4     col5     col6
1      date1    <int>    <int>    <int>    <int>    <int>    <int>
2      date1    <int>    <int>    <int>    <int>    <int>    <int>
3      date1    <int>    <int>    <int>    <int>    <int>    <int>
1      date2    <int>    <int>    <int>    <int>    <int>    <int>
2      date2    <int>    <int>    <int>    <int>    <int>    <int>
3      date2    <int>    <int>    <int>    <int>    <int>    <int>
id日期col1 col2 col3 col4 col5 col6
1日期1
2日期1
3日期1
1日期2
2日期2
3日期2

谢谢您的帮助。

您需要先在第一个组上使用合并功能,然后才能重新绑定数据帧。 使用data.table库,这是非常有效的:

Reduce(merge,df.list[1:3])
将给出合并的数据帧

   id  Date  col1  col2  col3  col4  col5  col6
1:  1 date1 <int> <int> <int> <int> <int> <int>
2:  2 date1 <int> <int> <int> <int> <int> <int>
3:  3 date1 <int> <int> <int> <int> <int> <int>
id日期col1 col2 col3 col4 col5 col6
1:1日期1
2:2日期1
3:3日期1
然后,您需要使用lappy创建一个列表,并将rbind应用于该列表

do.call("rbind",lapply(list(c(1:3),c(4:6)),function(x){Reduce(merge,df.list[x])}))

   id  Date  col1  col2  col3  col4  col5  col6
1:  1 date1 <int> <int> <int> <int> <int> <int>
2:  2 date1 <int> <int> <int> <int> <int> <int>
3:  3 date1 <int> <int> <int> <int> <int> <int>
4:  1 date2 <int> <int> <int> <int> <int> <int>
5:  2 date2 <int> <int> <int> <int> <int> <int>
6:  3 date2 <int> <int> <int> <int> <int> <int>
do.call(“rbind”,lappy(list(c(1:3),c(4:6)),函数(x){Reduce(merge,df.list[x]))
id日期col1 col2 col3 col4 col5 col6
1:1日期1
2:2日期1
3:3日期1
4:1日期2
5:2日期2
6:3日期2
数据:

library(data.table)
df.list <- list()

df.list[[1]] <- setDT(read.table(text = 
"  id     Date     col1     col2
  1      date1    <int>    <int>
  2      date1    <int>    <int>
  3      date1    <int>    <int>",
header = TRUE, stringsAsFactors = FALSE))

df.list[[2]] <- setDT(read.table(text = 
"  id     Date     col3     col4
  1      date1    <int>    <int>
  2      date1    <int>    <int>
  3      date1    <int>    <int>",
 header = TRUE, stringsAsFactors = FALSE))

df.list[[3]] <- setDT(read.table(text = 
"  id     Date     col5     col6
 1      date1    <int>    <int>
 2      date1    <int>    <int>
 3      date1    <int>    <int>",
 header = TRUE, stringsAsFactors = FALSE))


df.list[[4]] <- setDT(read.table(text = 
 "  id     Date     col1     col2
     1      date2    <int>    <int>
     2      date2    <int>    <int>
     3      date2    <int>    <int>",
     header = TRUE, stringsAsFactors = FALSE))

df.list[[5]] <- setDT(read.table(text = 
  "  id     Date     col3     col4
  1      date2    <int>    <int>
  2      date2    <int>    <int>
  3      date2    <int>    <int>",
  header = TRUE, stringsAsFactors = FALSE))

df.list[[6]] <- setDT(read.table(text = 
    "  id     Date     col5     col6
    1      date2    <int>    <int>
    2      date2    <int>    <int>
    3      date2    <int>    <int>",
    header = TRUE, stringsAsFactors = FALSE))
库(data.table)

df.list您需要先在第一个组上使用合并函数,然后才能重新绑定数据帧。 使用data.table库,这是非常有效的:

Reduce(merge,df.list[1:3])
将给出合并的数据帧

   id  Date  col1  col2  col3  col4  col5  col6
1:  1 date1 <int> <int> <int> <int> <int> <int>
2:  2 date1 <int> <int> <int> <int> <int> <int>
3:  3 date1 <int> <int> <int> <int> <int> <int>
id日期col1 col2 col3 col4 col5 col6
1:1日期1
2:2日期1
3:3日期1
然后,您需要使用lappy创建一个列表,并将rbind应用于该列表

do.call("rbind",lapply(list(c(1:3),c(4:6)),function(x){Reduce(merge,df.list[x])}))

   id  Date  col1  col2  col3  col4  col5  col6
1:  1 date1 <int> <int> <int> <int> <int> <int>
2:  2 date1 <int> <int> <int> <int> <int> <int>
3:  3 date1 <int> <int> <int> <int> <int> <int>
4:  1 date2 <int> <int> <int> <int> <int> <int>
5:  2 date2 <int> <int> <int> <int> <int> <int>
6:  3 date2 <int> <int> <int> <int> <int> <int>
do.call(“rbind”,lappy(list(c(1:3),c(4:6)),函数(x){Reduce(merge,df.list[x]))
id日期col1 col2 col3 col4 col5 col6
1:1日期1
2:2日期1
3:3日期1
4:1日期2
5:2日期2
6:3日期2
数据:

library(data.table)
df.list <- list()

df.list[[1]] <- setDT(read.table(text = 
"  id     Date     col1     col2
  1      date1    <int>    <int>
  2      date1    <int>    <int>
  3      date1    <int>    <int>",
header = TRUE, stringsAsFactors = FALSE))

df.list[[2]] <- setDT(read.table(text = 
"  id     Date     col3     col4
  1      date1    <int>    <int>
  2      date1    <int>    <int>
  3      date1    <int>    <int>",
 header = TRUE, stringsAsFactors = FALSE))

df.list[[3]] <- setDT(read.table(text = 
"  id     Date     col5     col6
 1      date1    <int>    <int>
 2      date1    <int>    <int>
 3      date1    <int>    <int>",
 header = TRUE, stringsAsFactors = FALSE))


df.list[[4]] <- setDT(read.table(text = 
 "  id     Date     col1     col2
     1      date2    <int>    <int>
     2      date2    <int>    <int>
     3      date2    <int>    <int>",
     header = TRUE, stringsAsFactors = FALSE))

df.list[[5]] <- setDT(read.table(text = 
  "  id     Date     col3     col4
  1      date2    <int>    <int>
  2      date2    <int>    <int>
  3      date2    <int>    <int>",
  header = TRUE, stringsAsFactors = FALSE))

df.list[[6]] <- setDT(read.table(text = 
    "  id     Date     col5     col6
    1      date2    <int>    <int>
    2      date2    <int>    <int>
    3      date2    <int>    <int>",
    header = TRUE, stringsAsFactors = FALSE))
库(data.table)

df.list这里是我认为你想要的tidyverse:

library(tidyverse)

ex_list <- list(df1, df2, df3, df4)

ex_list %>% 
  bind_rows() %>% 
  gather(measure, value, col1:col4) %>% 
  na.omit() %>% 
  spread(measure, value) %>% 
  arrange(date)


# A tibble: 6 x 6
     id       date  col1  col2  col3  col4
  <dbl>     <date> <dbl> <dbl> <dbl> <dbl>
1     1 2017-01-01     1     4     7    10
2     2 2017-01-01     2     5     8    11
3     3 2017-01-01     3     6     9    12
4     1 2017-01-02    11    14    21    24
5     2 2017-01-02    12    15    22    25
6     3 2017-01-02    13    16    23    26
库(tidyverse)
ex_列表%
绑定_行()%>%
聚集(度量值,值,col1:col4)%>%
na.省略()%>%
排列(测量值、值)%>%
安排(日期)
#一个tibble:6x6
id日期col1 col2 col3 col4
1     1 2017-01-01     1     4     7    10
2     2 2017-01-01     2     5     8    11
3     3 2017-01-01     3     6     9    12
4     1 2017-01-02    11    14    21    24
5     2 2017-01-02    12    15    22    25
6     3 2017-01-02    13    16    23    26
数据:


df1以下是我认为您在寻找tidyverse:

library(tidyverse)

ex_list <- list(df1, df2, df3, df4)

ex_list %>% 
  bind_rows() %>% 
  gather(measure, value, col1:col4) %>% 
  na.omit() %>% 
  spread(measure, value) %>% 
  arrange(date)


# A tibble: 6 x 6
     id       date  col1  col2  col3  col4
  <dbl>     <date> <dbl> <dbl> <dbl> <dbl>
1     1 2017-01-01     1     4     7    10
2     2 2017-01-01     2     5     8    11
3     3 2017-01-01     3     6     9    12
4     1 2017-01-02    11    14    21    24
5     2 2017-01-02    12    15    22    25
6     3 2017-01-02    13    16    23    26
库(tidyverse)
ex_列表%
绑定_行()%>%
聚集(度量值,值,col1:col4)%>%
na.省略()%>%
排列(测量值、值)%>%
安排(日期)
#一个tibble:6x6
id日期col1 col2 col3 col4
1     1 2017-01-01     1     4     7    10
2     2 2017-01-01     2     5     8    11
3     3 2017-01-01     3     6     9    12
4     1 2017-01-02    11    14    21    24
5     2 2017-01-02    12    15    22    25
6     3 2017-01-02    13    16    23    26
数据:


df1感谢您的输入。不过,并不是所有的组都有相同的大小。它们的大小从4到6不等。我需要以某种方式根据日期和id绑定它们。我不确定我是否理解您所说的组的含义。如果您的意思是要合并的数据帧的数量,那么您可以通过传递给lappy的向量来定义它(列表(c(1:3),c(4:6)),在我的答案和您的示例中)。如果第一组的编号为4,第二组的编号为6,则列表(c(1:4),c(5:11))应执行此操作。如果我误解了你的意思,试着编辑一下你的例子,给出一个更现实的例子