I am trying to take a date range and then determine whether the status is New, InProcess, or Ordered (complete) for each day in the date range. I have a start date and end date, and then get the days between. Then, I want to create a column for each day in the date range with the status for that day. So, both the column name and the case_when statements are dynamic based on the date range. The code below generates the three columns correctly, but doesn't fill them in except for the first. Is there a way to do this using purrr? Or is a loop the best way to go? How do I get it to calculate correctly over all columns?
start_date <- as.Date('2018-10-04')
end_date <- as.Date('2018-10-06')
date_range <- as.list(seq.Date(start_date, end_date, by = "day"))
for (i in seq_along(length(date_range))) {
test2 <- orderInfo %>%
mutate(!!paste0("day", i-1) := case_when(CompleteDate == as.Date(start_date + i-1) ~ "ORDERED",
CreateDate == as.Date(start_date + i-1) ~ "NEW",
CreateDate < as.Date(start_date + i-1) & (is.na(CompleteDate) | CompleteDate > as.Date(start_date + i-1)) ~ "InProcess"))
view(test2)
}
When I changed to 1:seq_along(date_range), I got an error that said the only the first number was used.
> for (i in 1:seq_along(date_range)) {
+
+
+ #orderInfo[ , paste0("day", i-1)] <- NA
+
+
+ test2 <- orderInfo %>%
+ mutate(!!paste0("day", i-1) := case_when(CompleteDate == (as.Date(start_date + i-1)) ~ "ORDERED",
+ CreateDate == as.Date(start_date + i-1) ~ "NEW",
+ CreateDate < as.Date(start_date + i-1) & (is.na(CompleteDate) | CompleteDate > as.Date(start_date + i-1)) ~ "InProcess"))
+ view(test2)
+
+ }
Error : numerical expression has 3 elements: only the first used
Any help is appreciated!!