无论前n行是否为正数/负数,求最大值?
嗨,我有一个这样的数据框。
#Data
df2 <- structure(list(Date = structure(c(18502, 18503, 18504, 18505,
18506, 18507, 18508, 18509, 18510, 18511, 18512, 18513, 18514,
18515, 18516, 18517, 18518, 18519, 18520, 18521, 18522, 18523,
18524, 18525, 18526, 18527, 18528, 18529, 18530, 18531, 18532,
18533, 18534, 18535, 18536, 18537, 18538, 18539, 18540), class = "Date"),
Price = c(1490, 3604, 2003, -4004, 4247, -189008, 2506, 4044,
2604, 2204, -4316, 2190, 3137, 2694, 711, 4075, 1315, 454,
1660, 4306, 4032, 3201, 2980, 4474, 3044, 3267, 2573, 2784,
1497, 897, 4342, 4086, 3192, 3634, 380, 2293, 3478, 1190,
1619)), class = "data.frame", row.names = c(NA, -39L))
我想要的是找到绝对值最大值而不仅仅是最大值。例如,如果前第 n 行设置为 4,则第 7 行将为 -189008。我尝试了两个函数,但都没有返回绝对值的选项。
df2 %>%
mutate(abshigh = slide_dbl(Price, max, .before = 4, .complete = TRUE))
zoo::rollmaxr (df2$Price,k=4,fill=NA)
上面的函数输出
Date Price abshigh
1 2020-08-28 1490 NA
2 2020-08-29 3604 NA
3 2020-08-30 2003 NA
4 2020-08-31 -4004 NA
5 2020-09-01 4247 4247
6 2020-09-02 -189008 4247
7 2020-09-03 2506 4247
8 2020-09-04 4044 4247
9 2020-09-05 2604 4247
10 2020-09-06 2204 4044
11 2020-09-07 -4316 4044
12 2020-09-08 2190 4044
13 2020-09-09 3137 3137
14 2020-09-10 2694 3137
15 2020-09-11 711 3137
16 2020-09-12 4075 4075
17 2020-09-13 1315 4075
18 2020-09-14 454 4075
19 2020-09-15 1660 4075
20 2020-09-16 4306 4306
21 2020-09-17 4032 4306
22 2020-09-18 3201 4306
23 2020-09-19 2980 4306
24 2020-09-20 4474 4474
25 2020-09-21 3044 4474
26 2020-09-22 3267 4474
27 2020-09-23 2573 4474
28 2020-09-24 2784 4474
29 2020-09-25 1497 3267
30 2020-09-26 897 3267
31 2020-09-27 4342 4342
32 2020-09-28 4086 4342
33 2020-09-29 3192 4342
34 2020-09-30 3634 4342
35 2020-10-01 380 4342
36 2020-10-02 2293 4086
37 2020-10-03 3478 3634
38 2020-10-04 1190 3634
39 2020-10-05 1619 3478
这是不正确的,因为第七行应该返回 -189008 而不是 4247 谢谢。
Hi I have a dataframe as such.
#Data
df2 <- structure(list(Date = structure(c(18502, 18503, 18504, 18505,
18506, 18507, 18508, 18509, 18510, 18511, 18512, 18513, 18514,
18515, 18516, 18517, 18518, 18519, 18520, 18521, 18522, 18523,
18524, 18525, 18526, 18527, 18528, 18529, 18530, 18531, 18532,
18533, 18534, 18535, 18536, 18537, 18538, 18539, 18540), class = "Date"),
Price = c(1490, 3604, 2003, -4004, 4247, -189008, 2506, 4044,
2604, 2204, -4316, 2190, 3137, 2694, 711, 4075, 1315, 454,
1660, 4306, 4032, 3201, 2980, 4474, 3044, 3267, 2573, 2784,
1497, 897, 4342, 4086, 3192, 3634, 380, 2293, 3478, 1190,
1619)), class = "data.frame", row.names = c(NA, -39L))
what I want is to find the absolute-value max and not just the max. For example, if the previous nth row is set to 4 then row 7 would be -189008. I have tried two functions but both do not have the option to return the absolute value.
df2 %>%
mutate(abshigh = slide_dbl(Price, max, .before = 4, .complete = TRUE))
zoo::rollmaxr (df2$Price,k=4,fill=NA)
above function outputs
Date Price abshigh
1 2020-08-28 1490 NA
2 2020-08-29 3604 NA
3 2020-08-30 2003 NA
4 2020-08-31 -4004 NA
5 2020-09-01 4247 4247
6 2020-09-02 -189008 4247
7 2020-09-03 2506 4247
8 2020-09-04 4044 4247
9 2020-09-05 2604 4247
10 2020-09-06 2204 4044
11 2020-09-07 -4316 4044
12 2020-09-08 2190 4044
13 2020-09-09 3137 3137
14 2020-09-10 2694 3137
15 2020-09-11 711 3137
16 2020-09-12 4075 4075
17 2020-09-13 1315 4075
18 2020-09-14 454 4075
19 2020-09-15 1660 4075
20 2020-09-16 4306 4306
21 2020-09-17 4032 4306
22 2020-09-18 3201 4306
23 2020-09-19 2980 4306
24 2020-09-20 4474 4474
25 2020-09-21 3044 4474
26 2020-09-22 3267 4474
27 2020-09-23 2573 4474
28 2020-09-24 2784 4474
29 2020-09-25 1497 3267
30 2020-09-26 897 3267
31 2020-09-27 4342 4342
32 2020-09-28 4086 4342
33 2020-09-29 3192 4342
34 2020-09-30 3634 4342
35 2020-10-01 380 4342
36 2020-10-02 2293 4086
37 2020-10-03 3478 3634
38 2020-10-04 1190 3634
39 2020-10-05 1619 3478
this is incorrect since row seven should return -189008 instead of 4247
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种选择是获取
abs
olute 'Price' 上的slide
max
,然后将sign
更改为 < code>match'Price' 的abs
olute 值(潜在错误警报!)-output
或者另一种选择是使用
row_number()
即序列为输入到slide_dbl
并根据序列提取 'Price' 的值,使用which.max
获取abs
olute 值的max
元素索引,以对“价格”子集(与之前的解决方案相比,这会更正确,因为如果存在具有相同值的负元素和正元素,则match
可能会对abs
绝对值产生不良后果价值输出
One option is to get the
slide
max
on theabs
olute 'Price' and then change thesign
bymatch
ing theabs
olute values of 'Price' (potential bug alert!)-output
Or another option is to use
row_number()
i.e. sequence as input toslide_dbl
and extract the values of 'Price' based on the sequence, get themax
element index ofabs
olute values withwhich.max
to subset the 'Price' subset (This would be more correct compared to the previous solution asmatch
can have undesirable consequence on theabs
olute values if there are both negative and positive elements with same value-output