【发布时间】:2021-12-30 06:47:03
【问题描述】:
巴西股票“PFRM3.SA”的雅虎财经历史价格存在一些错误。我试图使用dplyr 有条件地更改一个值。我设法使用case_when() 做到了这一点,但我无法使用replace() 使其工作,我认为这应该是一个更有效的解决方案。
library(tidyverse)
library(tidyquant)
price <- tq_get("PFRM3.SA", from = "2020-01-01", get = "stock.prices") #Data
#visual inspection
price %>%
ggplot(aes(x = date, y = close)) +
geom_line()
#the decimal digit was misplaced a few times
#this works fine
price %>%
mutate(close = case_when(close > 100 ~ close/100, TRUE ~ close))
#However I don't understand why this do not work
price %>%
mutate(close = replace(close, close > 100, close/100))
最后一行代码产生这个错误:
Warning messages:
1: Problem with `mutate()` input `close`.
ℹ number of items to replace is not a multiple of replacement length
ℹ Input `close` is `replace(close, close > 100, close/100)`.
2: In x[list] <- values :
number of items to replace is not a multiple of replacement length
我不明白为什么replace() 在不满足条件时不返回原始关闭值。另外,我不知道如何使它工作。谢谢
【问题讨论】: