I am trying to create a loop to create a dataframe that has a sum of a variable based on a rolling period of ten years. I think I got that bit figured, but I don't know how to rbind within the loop if the name is also assigned in the loop. So the loop just replaces itself.
dummy data setup:
library('dplyr')
library('sf')
library('tigris')
florida <- counties(state = "Florida", year = 2010) %>%
select(c(GEOID10)) %>% rename("geoid" = "GEOID10") %>% st_drop_geometry()
florida_annual <- florida %>% mutate(year = 1900, disaster1 = sample(0:5, 67, TRUE), disaster2 = sample(0:5, 67, TRUE) )
for(x in 1901:1990){
temp <- florida %>% mutate(year = x, disaster1 = sample(0:5, 67, TRUE), disaster2 = sample(0:5, 67, TRUE))
florida_annual <- rbind(florida_annual, temp)
rm(temp)
}
Loop that needs an rbinding effect somewhere:
for(x in 70){for(j in 1900:1910){
filename <- paste0("tab_", x)
assign(filename, florida_annual[florida_annual$year >= j & florida_annual$year <= j + x,] %>%
group_by(geoid) %>%
summarise(cat_total = sum(disaster1),
.groups = 'drop') %>% mutate(year_group = paste0(j,"_", j+x))
)
}
}