I have a list with dataframes that have the same number of columns and different number of rows. Like this example:
dfs_list <- list(data.frame(var1 = seq(1:10), var2 = LETTERS[1:10]),
data.frame(var1 = seq(1:20), var3 = LETTERS[1:20]),
data.frame(var1 = seq(1:15), var2 = LETTERS[1:15]))
To each of the data frames I need to add a column at the beginning according to a vector that I have with the cities
city <- c("city1", "city2", "city3")
Always the number of dataframes in dfs_list matches the number of elements of the vector city
I made this solution with a for loop
for (i in seq_along(city)) {
dfs_list[[i]] <- mutate(dfs_list[[i]], city = city[i], .before = 1)
}
I would like to know if there is another solution without the need to use the for loop, since I am finding it a bit slow when I generalize it for a large group of cities.