cars, bikes, motorcycle etc are other small dataframes that I would like to extract information for only one particular year and gather the information from all the different datasets into one. So the gathered data.frame for 2017 would be df_2017. The name of the columns are the years (2015, 2016, 2017)
Does anyone know how I can implement this in a FOR-loop (or by vectorization) so I dont have to write the code over and over again?
Here I'm illustrating on 3 toy data frames a, b and c. Note that naming each element of df_list before calling map_dfc() ensures that each output column's name matches the tibble from where that column was extracted.
library(tidyverse)
a <- tibble(`2017` = seq(1, 10), `2018` = seq(11, 20))
b <- tibble(`2017` = seq(21, 30), `2018` = seq(31, 40))
c <- tibble(`2017` = seq(41, 50), `2018` = seq(51, 60))
df_list <- list(a = a, b = b, c = c)
map_dfc(df_list, ~ .x$`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
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.
Okey, thanks! Especially on the comment on converting i to a character. That was VERY useful.
After doing that and going from $ to [[, my loop went through, but as you said it only made one data.frame. Is there a way to store the previous years?