在另一个数据框下添加数据框

时间:2017-05-31 05:55:35

标签: r

我想做以下事情:

我有一个实际销售数据框

Dates       Actual
24/04/2017  58
25/04/2017  59
26/04/2017  58
27/04/2017  154
28/04/2017  117
29/04/2017  127
30/04/2017  178

预测值的另一个数据框

Dates           Predicted
01/05/2017      68.54159
02/05/2017      90.7313
03/05/2017      82.76875
04/05/2017      117.48913
05/05/2017      110.3809
06/05/2017      156.53363
07/05/2017      198.14819

以下列方式将预测销售数据框添加到实际数据框下方:

Dates      Actual   Predicted
24/04/2017  58  
25/04/2017  59  
26/04/2017  58  
27/04/2017  154 
28/04/2017  117 
29/04/2017  127 
30/04/2017  178 
01/05/2017        68.54159
02/05/2017        90.7313
03/05/2017        82.76875
04/05/2017        117.48913
05/05/2017        110.3809
06/05/2017        156.53363
07/05/2017        198.14819

1 个答案:

答案 0 :(得分:2)

使用:

library(dplyr)
bind_rows(d1, d2)

你得到:

        Dates Actual Predicted
1  24/04/2017     58        NA
2  25/04/2017     59        NA
3  26/04/2017     58        NA
4  27/04/2017    154        NA
5  28/04/2017    117        NA
6  29/04/2017    127        NA
7  30/04/2017    178        NA
8  01/05/2017     NA  68.54159
9  02/05/2017     NA  90.73130
10 03/05/2017     NA  82.76875
11 04/05/2017     NA 117.48913
12 05/05/2017     NA 110.38090
13 06/05/2017     NA 156.53363
14 07/05/2017     NA 198.14819

或者用:

library(data.table)
rbindlist(list(d1,d2), fill = TRUE)

或者用:

library(plyr)
rbind.fill(d1,d2)