I currently have the following dataset in r
geoid <- c(100, 200, 300, 400, 100, 200, 300, 400)
variable <- c("total_pop", "white", "black", "aian", "total_pop", "white", "black", "aian")
estimate <- c(100, 200, 300, 400, 200, 300, 400, 500)
df <- data.frame(geoid, variable, estimate)
I am trying to create four separate datasets. Each dataset contains the rows from the variable column e.g. one dataset with the total_pop values, one dataset with the white values, et cetera. I am currently doing that with the following loop
variable <- c("total_pop", "white", "black", "aian")
for (x in variable) {
x <- df %>%
filter(variable == x)
This will create a dataframe but will automatically override the previous dataframe e.g. at the end, only the aian dataframe will remain. How can I rewrite this so that it won't overwrite the previous dataframe?