There are some mistakes in the Yahoo finance historical price for the Brazilian stock "PFRM3.SA". I was trying to conditionally change a value using dplyr. I managed to do it using case_when() but I couldn't make it work using replace(), which I think should be a more efficient solution.
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))
The last line of code generates this error:
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
I don't understand why replace() doesn't return the original close value when the condition isn't met. Also, I don't know how to make it work. Thanks
