I have a time series looking like:
id date type
1 1 2019-06-16 1_month
2 1 2019-07-16 1_month
3 1 2019-08-08 1_month
4 1 2019-09-04 3_months
5 2 2019-01-23 1_month
6 2 2019-05-05 1_month
7 2 2019-07-10 3_months
8 3 2019-07-02 1_month
9 3 2020-04-18 3_months
10 4 2019-03-10 1_month
11 4 2019-04-08 1_month
library(tidyverse)
df <- data.frame(stringsAsFactors=FALSE,
id = c(1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4),
date = c("2019-06-16", "2019-07-16", "2019-08-08", "2019-09-04",
"2019-01-23", "2019-05-05", "2019-07-10",
"2019-07-02", "2020-04-18", "2019-03-10", "2019-04-08"),
type = c("1_month", "1_month", "1_month", "3_months", "1_month",
"1_month", "3_months", "1_month", "3_months",
"1_month", "1_month")
)
For each id, there are some entries "1_month" - aka with a monthly length - and "3_months" entries.
I would like to expand the "3_months" entries into triplets of "1_month" entires, and the triplets should have a 30 days interval between them as in the example.
Desired result:
df_r <- data.frame(stringsAsFactors=FALSE,
id = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3),
date = c("2019-06-16", "2019-07-16", "2019-08-08", "2019-09-04",
"2019-10-04", "2019-11-03", "2019-01-23",
"2019-05-05", "2019-07-10", "2019-08-09", "2019-09-08",
"2019-07-02", "2020-04-18", "2020-05-18", "2020-06-17"),
type = c("1_month", "1_month", "1_month", "1_month", "1_month",
"1_month", "1_month", "1_month", "1_month", "1_month",
"1_month", "1_month", "1_month", "1_month", "1_month")
)
id date type
1 1 2019-06-16 1_month
2 1 2019-07-16 1_month
3 1 2019-08-08 1_month
4 1 2019-09-04 1_month
5 1 2019-10-04 1_month
6 1 2019-11-03 1_month
7 2 2019-01-23 1_month
8 2 2019-05-05 1_month
9 2 2019-07-10 1_month
10 2 2019-08-09 1_month
11 2 2019-09-08 1_month
12 3 2019-07-02 1_month
13 3 2020-04-18 1_month
14 3 2020-05-18 1_month
15 3 2020-06-17 1_month
What I thought:
- First isolate the 3_months entries
for each of them, group by user_id and then use
completeas follows:df %>% filter(type == '3_months') %>% group_by(id) %>% complete(id, date = seq(date, by = "30 days", length.out = 3))
But I get:
Error in seq.default(date, by = "30 days", length.out = 3) :
'from' must be a finite number
Thanks for your help
