data.frame(
scrape_date = as.Date(c("2023-01-01", "2023-01-02", "2023-01-03", "2023-01-04", "2023-01-05", "2023-01-06", "2023-01-07", "2023-01-08")),
own_product_id = c("00617","00617","00617","00617","00617","00617","00617","00617"),
own_price = c(70, 70, 70, 70, 70,70,70,71),
comp_price = c(70, 71, 71, 71, 71,71,71,71)
)
如果我想计算从 comp_price 变化到 own_price 相同需要多少天?我该怎么做呢?抱歉,这让我很头疼
期待类似的输出
product_id time_taken_to_match
00617 6 days
您可以使用
cumsum()
来确定 own_price
何时赶上 comp_price
。
library(dplyr)
df %>%
group_by(own_product_id, grp = cumsum(own_price == comp_price)) %>%
summarise(time_taken_to_match = diff(range(scrape_date)), .groups = "drop") %>%
filter(time_taken_to_match > 0)
# # A tibble: 1 × 3
# own_product_id grp time_taken_to_match
# <chr> <int> <drtn>
# 1 00617 1 6 days