R:如何将这两个数据框组合成一个按日期列的数据框?

时间:2017-06-09 03:35:35

标签: r datetime dataframe

我是新练习R,现在陷入困境已经好几天不能继续了。 我选择了我记录的数据进行分析。从2个示例设备每小时记录一次数据,并且记录设备与数据库服务器之间存在一些小的延迟问题,因此数小时可以在秒0(+ -5或保持变化非常小)的情况下记录数据。你可以在示例数据中清楚地看到。

重大问题

1)Date列中的某些行在秒部分不相等。

2)在我通过head(df1,24)/ head(df2,24)选择了示例数据之后,在Date中有一些行缺少行我在df2'找到了丢失的行,并且得到了结果喜欢在这里。如果有10000个选定行将成为许多缺失值的大问题。

> class(df1$Date)
[1] "POSIXct" "POSIXt" 

DF1

> df1
                  Date Val   Weekday
1  2017-06-08 13:00:04 520  Thursday
2  2017-06-08 12:00:04 507  Thursday
3  2017-06-08 11:00:04 501  Thursday
4  2017-06-08 10:00:04 472  Thursday
5  2017-06-08 09:00:04 444  Thursday
6  2017-06-08 08:00:04 446  Thursday
7  2017-06-08 07:00:04 444  Thursday
8  2017-06-08 06:00:04 441  Thursday
9  2017-06-08 05:00:04 439  Thursday
10 2017-06-08 04:00:04 442  Thursday
11 2017-06-08 03:00:04 440  Thursday
12 2017-06-08 02:00:04 443  Thursday
13 2017-06-08 01:00:04 445  Thursday
14 2017-06-08 00:00:04 445  Thursday
15 2017-06-07 23:00:04 446 Wednesday
16 2017-06-07 22:00:04 445 Wednesday
17 2017-06-07 21:00:04 447 Wednesday
18 2017-06-07 20:00:04 445 Wednesday
19 2017-06-07 19:00:04 451 Wednesday
20 2017-06-07 18:00:04 469 Wednesday
21 2017-06-07 17:00:04 473 Wednesday
22 2017-06-07 16:00:04 510 Wednesday
23 2017-06-07 15:00:04 517 Wednesday
24 2017-06-07 14:00:04 524 Wednesday

DF2

> df2
                  Date df1.Val   Weekday
1  2017-06-08 12:00:04 507  Thursday
2  2017-06-08 11:00:04 501  Thursday
3  2017-06-08 10:00:04 472  Thursday
4  2017-06-08 09:00:04 444  Thursday
5  2017-06-08 08:00:04 446  Thursday
6  2017-06-08 07:00:04 444  Thursday
7  2017-06-08 06:00:04 441  Thursday
8  2017-06-08 05:00:04 439  Thursday
9  2017-06-08 04:00:04 442  Thursday
10 2017-06-08 03:00:04 440  Thursday
11 2017-06-08 02:00:04 443  Thursday
12 2017-06-08 01:00:04 445  Thursday
13 2017-06-08 00:00:04 445  Thursday
14 2017-06-07 23:00:04 446 Wednesday
15 2017-06-07 22:00:04 445 Wednesday
16 2017-06-07 21:00:04 447 Wednesday
17 2017-06-07 20:00:04 445 Wednesday
18 2017-06-07 19:00:04 451 Wednesday
19 2017-06-07 18:00:04 469 Wednesday
20 2017-06-07 17:00:04 473 Wednesday
21 2017-06-07 16:00:03 510 Wednesday
22 2017-06-07 15:00:03 517 Wednesday
23 2017-06-07 14:00:03 524 Wednesday
24 2017-06-07 13:00:03 489 Wednesday
  

那么,如何准备数据以便能够组合数据帧?

我希望输出如下例所示:

                  Date df1.Val df2.Val   Weekday
   2017-06-08 13:00:04 520              Thursday
   2017-06-08 12:00:00 507     507      Thursday
   2017-06-08 11:00:00 501     501      Thursday
   2017-06-08 10:00:00 472     472      Thursday
   2017-06-08 09:00:00 444     444      Thursday
   2017-06-08 08:00:00 446     446      Thursday
   2017-06-08 07:00:00 444     444      Thursday
   2017-06-08 06:00:00 441     441      Thursday
   2017-06-08 05:00:00 439     439      Thursday
   2017-06-08 04:00:00 442     442      Thursday
   2017-06-08 03:00:00 440     440      Thursday
   2017-06-08 02:00:00 443     443      Thursday
   2017-06-08 01:00:00 445     445      Thursday
   2017-06-08 00:00:00 445     445     Thursday
   2017-06-07 23:00:00 446     446     Wednesday
   2017-06-07 22:00:00 445     445     Wednesday
   2017-06-07 21:00:00 447     447     Wednesday
   2017-06-07 20:00:00 445     445     Wednesday
   2017-06-07 19:00:00 451     451     Wednesday
   2017-06-07 18:00:00 469     469     Wednesday
   2017-06-07 17:00:00 473     473     Wednesday
   2017-06-07 16:00:00 510     510     Wednesday
   2017-06-07 15:00:00 517     517     Wednesday
   2017-06-07 14:00:00 524     524     Wednesday
   2017-06-07 13:00:00         489     Wednesday

但这些对Val来说是相同的,因为如果来自不同来源的设备从相同的源获得val,则假设它将是这样的:

                  Date df1.Val df3.Val   Weekday
   2017-06-08 13:00:04 520     550      Thursday
   2017-06-08 12:00:00 507     515      Thursday
   2017-06-08 11:00:00 501     496      Thursday

请帮忙。非常感谢你。

1 个答案:

答案 0 :(得分:0)

现在应该可以了。有一点需要注意,新数据框会自动排序。:

state.userInfo = Object.assign({}, state.userInfo, data)