我有一份收盘价和日期清单。
ï..Date Close
1 3-Nov-17 172.50
2 2-Nov-17 168.11
3 1-Nov-17 166.89
4 31-Oct-17 169.04
5 30-Oct-17 166.72
6 27-Oct-17 163.05
7 26-Oct-17 157.41
8 25-Oct-17 156.41
9 24-Oct-17 157.10
10 23-Oct-17 156.17
11 20-Oct-17 156.25
12 19-Oct-17 155.98
13 18-Oct-17 159.76
14 17-Oct-17 160.47
15 16-Oct-17 159.88
16 13-Oct-17 156.99
17 12-Oct-17 156.00
18 11-Oct-17 156.55
19 10-Oct-17 155.90
20 9-Oct-17 155.84
我知道如何应用过滤器,但我真正想做的是比较每天的收盘价,如果下一个价格高于当前价格,我想将其添加到不断增长的子集中。或者,如果当前价格低于之前的价格,我想将其添加到不同的子集。
这两个子集基本上就是这样。
3-Nov-17 172.5 B
2-Nov-17 168.11 B
31-Oct-17 169.04 B
30-Oct-17 166.72 B
27-Oct-17 163.05 B
26-Oct-17 157.41 B
24-Oct-17 157.1 B
20-Oct-17 156.25 B
17-Oct-17 160.47 B
16-Oct-17 159.88 B
13-Oct-17 156.99 B
11-Oct-17 156.55 B
10-Oct-17 155.9 B
9-Oct-17 155.84 B
另外
1-Nov-17 166.89 S
25-Oct-17 156.41 S
23-Oct-17 156.17 S
19-Oct-17 155.98 S
18-Oct-17 159.76 S
12-Oct-17 156 S
答案 0 :(得分:1)
library(dplyr)
dt = read.table(text="
Date Close
1 3-Nov-17 172.50
2 2-Nov-17 168.11
3 1-Nov-17 166.89
4 31-Oct-17 169.04
5 30-Oct-17 166.72
6 27-Oct-17 163.05
7 26-Oct-17 157.41
8 25-Oct-17 156.41
9 24-Oct-17 157.10
10 23-Oct-17 156.17
11 20-Oct-17 156.25
12 19-Oct-17 155.98
13 18-Oct-17 159.76
14 17-Oct-17 160.47
15 16-Oct-17 159.88
16 13-Oct-17 156.99
17 12-Oct-17 156.00
18 11-Oct-17 156.55
19 10-Oct-17 155.90
20 9-Oct-17 155.84
")
dt %>% mutate(group = ifelse(Close < lead(Close), "S", "B"))
# Date Close group
# 1 3-Nov-17 172.50 B
# 2 2-Nov-17 168.11 B
# 3 1-Nov-17 166.89 S
# 4 31-Oct-17 169.04 B
# 5 30-Oct-17 166.72 B
# 6 27-Oct-17 163.05 B
# 7 26-Oct-17 157.41 B
# 8 25-Oct-17 156.41 S
# 9 24-Oct-17 157.10 B
# 10 23-Oct-17 156.17 S
# 11 20-Oct-17 156.25 B
# 12 19-Oct-17 155.98 S
# 13 18-Oct-17 159.76 S
# 14 17-Oct-17 160.47 B
# 15 16-Oct-17 159.88 B
# 16 13-Oct-17 156.99 B
# 17 12-Oct-17 156.00 S
# 18 11-Oct-17 156.55 B
# 19 10-Oct-17 155.90 B
# 20 9-Oct-17 155.84 <NA>
您可以按列group
拆分更新的数据集。不知道最后一行应该属于哪里,因为你没有下一个要与之比较的值。