I have a list of elements that hold values in them. I would to write an if statement where if their isn't a specific number of elements for a specific ID (e.g., A, B, C) then add the appropriate number of elements, and assign an NA as the value in the element. I would like the output to look something like expected_ID. Is there an efficient way of doing this?
library(lubridate)
library(tidyverse)
library(purrr)
date <- rep_len(seq(dmy("01-01-2011"), dmy("31-07-2011"), by = "days"), 200)
ID <- rep(c("A","B", "C"), 200)
df <- data.frame(date = date,
x = runif(length(date), min = 60000, max = 80000),
y = runif(length(date), min = 800000, max = 900000),
ID)
df$Month <- month(df$date)
int1 <- df %>%
# arrange(ID) %>% # skipped for readability of result
mutate(new = floor_date(date, '10 day')) %>%
mutate(new = if_else(day(new) == 31, new - days(10), new)) %>%
group_by(ID, new) %>%
filter(Month == "1") %>%
group_split()
names(int1) <- sapply(int1, function(x) paste(x$ID[1]))
int1 <- int1[-c(6, 8, 9)]
expected_ID <- list(int1[[1]], int1[[2]], int1[[3]], int1[[4]], int1[[5]], NA, int1[[6]], NA, NA)
names(expected_ID) <- c(rep("A", 3), rep("B", 3), rep("C", 3))