使用R,我试图使用相同的字段但具有重叠的行来组合2个连续但不规则的时序数据集。也就是说,一些相同的交易记录同时出现在两个数据集中,我想消除重叠的行。
由于时间间隔是不规则的,因此我在每个数据集中可能有有效的相同行。对于我的示例数据集,我想将数据集1中的第1到12行与数据集2中的第6到11行合并,以获得所需的结果。在此示例中,很明显,数据集2的第1到5行与数据集1的第8到12行相同。我尝试使用unique()函数,但它也消除了相同的有效行。关于如何解决这一难题的任何想法?
数据集1
1 2019-02-19 15:17:14 25886 1
2 2019-02-19 15:17:14 25886 1
3 2019-02-19 15:17:15 25885 1
4 2019-02-19 15:17:16 25886 2
5 2019-02-19 15:17:16 25886 1
6 2019-02-19 15:17:16 25886 2
7 2019-02-19 15:17:16 25886 1
8 2019-02-19 15:17:18 25885 4
9 2019-02-19 15:17:19 25885 1
10 2019-02-19 15:17:19 25885 1
11 2019-02-19 15:17:20 25885 2
12 2019-02-19 15:17:21 25885 1
数据集2
1 2019-02-19 15:17:18 25885 4
2 2019-02-19 15:17:19 25885 1
3 2019-02-19 15:17:19 25885 1
4 2019-02-19 15:17:20 25885 2
5 2019-02-19 15:17:21 25885 1
6 2019-02-19 15:17:23 25886 2
7 2019-02-19 15:17:23 25886 3
8 2019-02-19 15:17:23 25886 3
9 2019-02-19 15:17:23 25886 1
10 2019-02-19 15:17:23 25886 1
11 2019-02-19 15:17:23 25886 2
我想要的结果是:
1 2019-02-19 15:17:14 25886 1
2 2019-02-19 15:17:14 25886 1
3 2019-02-19 15:17:15 25885 1
4 2019-02-19 15:17:16 25886 2
5 2019-02-19 15:17:16 25886 1
6 2019-02-19 15:17:16 25886 2
7 2019-02-19 15:17:16 25886 1
8 2019-02-19 15:17:18 25885 4
9 2019-02-19 15:17:19 25885 1
10 2019-02-19 15:17:19 25885 1
11 2019-02-19 15:17:20 25885 2
12 2019-02-19 15:17:21 25885 1
13 2019-02-19 15:17:23 25886 2
14 2019-02-19 15:17:23 25886 3
15 2019-02-19 15:17:23 25886 3
16 2019-02-19 15:17:23 25886 1
17 2019-02-19 15:17:23 25886 1
18 2019-02-19 15:17:23 25886 2
这里的数据集1
structure(list(time = structure(c(1550589434, 1550589434, 1550589435,
1550589436, 1550589436, 1550589436, 1550589436, 1550589438, 1550589439,
1550589439, 1550589440, 1550589441), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), price = c(25886, 25886, 25885, 25886, 25886,
25886, 25886, 25885, 25885, 25885, 25885, 25885), size = c(1,
1, 1, 2, 1, 2, 1, 4, 1, 1, 2, 1)), row.names = c("1", "2", "3",
"4", "5", "6", "7", "8", "9", "10", "11", "12"), class = "data.frame")
这里的数据集2
structure(list(time = structure(c(1550589438, 1550589439, 1550589439,
1550589440, 1550589441, 1550589443, 1550589443, 1550589443, 1550589443,
1550589443, 1550589443), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
price = c(25885, 25885, 25885, 25885, 25885, 25886, 25886,
25886, 25886, 25886, 25886), size = c(4, 1, 1, 2, 1, 2, 3,
3, 1, 1, 2)), row.names = c("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11"), class = "data.frame")
答案 0 :(得分:0)
一个想法是:
library(dplyr)
df2 %>%
anti_join(df1) %>%
bind_rows(df1)