如何合并包含R中列表值的2个不同的数据帧

时间:2014-01-07 10:13:13

标签: r

我有两组列表(OSC_LW和SCnFO),str()如下所示:

> str(OSC_LW)
List of 72
$ 1008/1009          :'data.frame':    8 obs. of  4 variables:
..$ StockCode : Factor w/ 8 levels "CMB00011","IFP00001",..: 1 2 3 7 4 5 6 8
..$ Opening_LW: int [1:8] 29 18 15 21 1 3 5 3
..$ Sales_LW  : int [1:8] 5 17 3 11 0 1 1 3
..$ Closing_LW: int [1:8] 24 1 12 10 1 2 4 0
$ 1012/1013          :'data.frame':    8 obs. of  4 variables:
..$ StockCode : Factor w/ 8 levels "CMB00011","IFP00001",..: 1 2 3 7 4 5 6 8
..$ Opening_LW: int [1:8] 18 23 14 11 1 3 4 5
..$ Sales_LW  : int [1:8] 12 16 13 9 1 0 1 0
..$ Closing_LW: int [1:8] 6 7 1 2 0 3 3 5
$ 1016/1017          :'data.frame':    8 obs. of  4 variables:
..$ StockCode : Factor w/ 8 levels "CMB00011","IFP00001",..: 1 2 3 7 4 5 6 8
..$ Opening_LW: int [1:8] 20 25 15 18 1 4 4 4
..$ Sales_LW  : int [1:8] 12 22 14 16 1 2 2 4
..$ Closing_LW: int [1:8] 8 3 1 2 0 2 2 0

> str(SCnFO)
List of 72
$ 1008/1009          :'data.frame':    8 obs. of  2 variables:
..$ StockCode: Factor w/ 8 levels "CMB00011","IFP00001",..: 1 2 3 7 4 5 6 8
..$ Opening_T: num [1:8] 5 17 3 11 1 1 1 4
$ 1012/1013          :'data.frame':    8 obs. of  2 variables:
..$ StockCode: Factor w/ 8 levels "CMB00011","IFP00001",..: 1 2 3 7 4 5 6 8
..$ Opening_T: num [1:8] 12 16 13 9 2 1 1 1
$ 1016/1017          :'data.frame':    8 obs. of  2 variables:
..$ StockCode: Factor w/ 8 levels "CMB00011","IFP00001",..: 1 2 3 7 4 5 6 8
..$ Opening_T: num [1:8] 12 22 14 16 2 2 2 5

为了让那些想要尝试它的人可以重复,我用下面的代码重新创建我的数据(只是原始数据的一部分,我的原始数据包含每个72的List):

OSC_LW

{
`1008/1009`<-data.frame(
    StockCode=c('CMB00011','IFP00001','IFP00003','IFP00046','IFP00016','IFP00033','IFP00042','IFP00047'),
    Opening_LW=c('29','18','15','21','1','3','5','3'),
    Sales_LW=c('5','17','3','11','0','1','1','3'),
    Closing_LW=c('24','1','12','10','1','2','4','0'))

`1012/1013`<-data.frame(
    StockCode=c('CMB00011','IFP00001','IFP00003','IFP00046','IFP00016','IFP00033','IFP00042','IFP00047'),
    Opening_LW=c('18','23','14','11','1','3','4','5'),
    Sales_LW=c('12','16','13','9','1','0','1','0'),
    Closing_LW=c('6','7','1','2','0','3','3','5'))

`1016/1017`<-data.frame(
    StockCode=c('CMB00011','IFP00001','IFP00003','IFP00046','IFP00016','IFP00033','IFP00042','IFP00047'),
    Opening_LW=c('20','25','15','18','1','4','4','4'),
    Sales_LW=c('12','22','14','16','1','2','2','4'),
    Closing_LW=c('8','3','1','2','0','2','2','0'))

OSC_LW<-list(`1008/1009`=`1008/1009`,`1012/1013`=`1012/1013`,`1016/1017`=`1016/1017`)
OSC_LW
}

SCnFO

{
`1008/1009`<-data.frame(
    StockCode=c('CMB00011','IFP00001','IFP00003','IFP00046','IFP00016','IFP00033','IFP00042','IFP00047'),
    Opening_T=c('5','17','3','11','1','1','1','4'))

`1012/1013`<-data.frame(
    StockCode=c('CMB00011','IFP00001','IFP00003','IFP00046','IFP00016','IFP00033','IFP00042','IFP00047'),
    Opening_T=c('12','16','13','9','2','1','1','1'))

`1016/1017`<-data.frame(
    StockCode=c('CMB00011','IFP00001','IFP00003','IFP00046','IFP00016','IFP00033','IFP00042','IFP00047'),
    Opening_T=c('12','22','14','16','2','2','2','5'))

SCnFO<-list(`1008/1009`=`1008/1009`,`1012/1013`=`1012/1013`,`1016/1017`=`1016/1017`)
SCnFO
}

我想要做的是将这两个数据(OSC_LW和SCnFO)合并为“StockCode”并将结果返回到列表中。

任何人都可以帮助我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

首先将列表中的所有data.frames合并为一个data.frame:

for (i in seq_along(OSC_LW)) {
  OSC_LW[[i]]$id <- names(OSC_LW)[i]
}
OSC_LW <- do.call(rbind, OSC_LW)

for (i in seq_along(SCnFO)) {
  SCnFO[[i]]$id <- names(SCnFO)[i]
}
SCnFO <- do.call(rbind, SCnFO)

现在您可以轻松合并:

merge(OSC_LW, SCnFO, by=c("id", "StockCode"))

#           id StockCode Opening_LW Sales_LW Closing_LW Opening_T
# 1  1008/1009  CMB00011         29        5         24         5
# 2  1008/1009  IFP00001         18       17          1        17
# 3  1008/1009  IFP00003         15        3         12         3
# 4  1008/1009  IFP00016          1        0          1         1
# 5  1008/1009  IFP00033          3        1          2         1
# 6  1008/1009  IFP00042          5        1          4         1
# 7  1008/1009  IFP00046         21       11         10        11
# 8  1008/1009  IFP00047          3        3          0         4
# 9  1012/1013  CMB00011         18       12          6        12
# 10 1012/1013  IFP00001         23       16          7        16
# 11 1012/1013  IFP00003         14       13          1        13
# 12 1012/1013  IFP00016          1        1          0         2
# 13 1012/1013  IFP00033          3        0          3         1
# 14 1012/1013  IFP00042          4        1          3         1
# 15 1012/1013  IFP00046         11        9          2         9
# 16 1012/1013  IFP00047          5        0          5         1
# 17 1016/1017  CMB00011         20       12          8        12
# 18 1016/1017  IFP00001         25       22          3        22
# 19 1016/1017  IFP00003         15       14          1        14
# 20 1016/1017  IFP00016          1        1          0         2
# 21 1016/1017  IFP00033          4        2          2         2
# 22 1016/1017  IFP00042          4        2          2         2
# 23 1016/1017  IFP00046         18       16          2        16
# 24 1016/1017  IFP00047          4        4          0         5

如果你必须(为什么会这样?)你可以再次分割结果。