【发布时间】:2020-09-09 04:08:45
【问题描述】:
我有一个数据框如下:
country day value
AE 1 23
AE 2 30
AE 3 21
AE 4 3
BD 1 2
BD 2 23
... .. ..
BD 22 23
我想从 2020-08-01 到 2020-08-21 的开始日期将日期列填充到我的数据框中 对于每个组。 这是我的尝试:
values = seq(from = as.Date("2020-08-01"), to = as.Date("2020-08-21"), by = 'day')
df<- df %>% group_by(country) %>% mutate(date=values)
但它没有给我正确的结果。
这是我想要的结果:
国家起息日
AE 1 23 2020-08-01
AE 2 30 2020-08-02
AE 3 21 2020-08-03
AE 4 3 2020-08-04
BD 1 2 2020-08-01
BD 2 23 2020-08-02
... .. ..
BD 21 23 2020-08-21
请告诉我如何解决这个问题。 这是错误:
Error: Problem with `mutate()` input `date`.
x Input `date` can't be recycled to size 23.
ℹ Input `date` is `seq(...)`.
ℹ Input `date` must be size 23 or 1, not 23.
ℹ The error occured in group 22: country = "CU".
Run `rlang::last_error()` to see where the error occurred.
【问题讨论】: