Continuing the same example provided by Siddharth, you can do something like this:
library(purrr)
library(tibble)
a <- tibble(`2017` = 1:10, `2018` = 11:20)
b <- tibble(`2017` = 21:30, `2018` = 31:40)
c <- tibble(`2017` = 41:50, `2018` = 51:60)
df_list <- list(a = a, b = b, c = c)
year_list <- c(2017, 2018)
walk(.x = year_list,
.f = function(year)
{
assign(x = paste("df", year,
sep = "_"),
value = map_dfc(.x = df_list,
.f = ~ .x[[as.character(x = year)]]),
envir = .GlobalEnv)
})
df_2017
#> # A tibble: 10 x 3
#> a b c
#> <int> <int> <int>
#> 1 1 21 41
#> 2 2 22 42
#> 3 3 23 43
#> 4 4 24 44
#> 5 5 25 45
#> 6 6 26 46
#> 7 7 27 47
#> 8 8 28 48
#> 9 9 29 49
#> 10 10 30 50
df_2018
#> # A tibble: 10 x 3
#> a b c
#> <int> <int> <int>
#> 1 11 31 51
#> 2 12 32 52
#> 3 13 33 53
#> 4 14 34 54
#> 5 15 35 55
#> 6 16 36 56
#> 7 17 37 57
#> 8 18 38 58
#> 9 19 39 59
#> 10 20 40 60
There are two reasons for which your code fails.
First one is that df_i won't recognise the year, it'll simply use a variable df_i for all year, so data.frame for each year will replace the previous one. You don't want to do that.
Second, your code tries to access the column with name literally i, and not it's value. To use the value, `[[` is the better function than `$`. Also, you have to convert i to a character, as column names are character and not integer.
Hope this helps.