I have a data frame which I want split into several elements of a named list by filtering by year and selecting a single variable.
df <- data.frame(year = rep(2010:2020, times=3), x = runif(33))
What I want to get is something similar to
yr2010 <- df %>%
filter(year == 2010) %>%
select(x) %>%
as.matrix()
yr2011 <- df %>%
filter(year == 2011) %>%
select(x) %>%
as.matrix()
...
yr2020 <- df %>%
filter(year == 2020) %>%
select(x) %>%
as.matrix()
df_list <- list(yr2010=yr2010, ..., yr2020=yr2020)
How can I do that?