I have a dataset of ids, months and some metric x.
library(tidyverse)
library(lubridate)
df <- data.frame(
id = c("a1", "a1", "a1", "b1", "b1", "b1"),
month = c("2021-01-01", "2021-03-01", "2021-04-01", "2021-05-01", "2021-06-01", "2021-08-01"),
x = c(34,56,76, 12, 13, 14),
month_start = c("2021-01-01", "2021-01-01", "2021-01-01", "2021-05-01", "2021-05-01", "2021-05-01")
)
df <- df %>% mutate(month = as.Date(month), month_start = as.Date(month_start))
and would like to create new columns that are values of the 2nd and 4th months' x of each customer. I tried the code below but it failed because for id == "a1"
, the 2nd month's x is NA. (error: Input x_growth_start
can't be recycled to size 3.)
df %>%
group_by(id) %>%
mutate(x_growth_start = x[month == month_start %m+% months(1)],
x_growth_end = x[month == month_start %m+% months(3)])
I realize what it actually returns when the value doesn't exist is integer(0)
. Can I make the code run (say, by letting it return NA
if integer(0))
? I tried what was suggested here but it didn't work Input `typ` can't be recycled to size in R
Thanks in advance.