I have a list of dataframes and I want to create a new column in each of those dataframes that is the name of the dataframe, which I also have in another list. I'm not sure why but it ends up creating a new dataframe that only consists of the last dataframe in my list.
schema <- "x"
table_prefix <- "results_"
geos <- district %>% filter(geo != "geo")
for (geo in geos){
tables <- paste0(schema, ".", table_prefix, geo)
queries <- paste("SELECT * FROM", tables)
}
i = 1
df_list <- list()
for (x in queries){
name <- substr(x, 40, nchar(x))
df_list[i] <- name
assign(name, dbGetQuery(con, x))
i = i + 1
}
dfs <- lapply(df_list, get)
i = 1
for (x in dfs){
x$geo <- df_list[i]
i = i + 1
}
full_df <- do.call(rbind, dfs)
This ends up giving me a new dataframe called 'x' that has the contents of the last dataframe in my list with the new column I want. How can I get it so all the dataframes get a new column with their name?