I am working with 11 different data frames in R, each with several columns of data on U.S. counties. Each observation, therefore, pertains to one county. The data frames are of different lengths, indicating that some data frames have entries for counties that other data frames do not. All data frames have a “FIPS” code as the value in one column, which indicates which U.S. county the observation gives data on. I want to reduce the length of all 11 data frames to identical lengths, with identical vectors of FIPS codes.
To illustrate, two data frames are called “Gini” and “Crime2022.” I used this code to reduce them to common lengths:
Gini1 <- Gini[Gini$FIPS %in% c(Crime2018$FIPS) , ]
Crime2018_1 <- Crime2018[Crime2018$FIPS %in% c(Gini$FIPS) , ]
This worked, but with 11 data frames, there will be many iterations of this code. Is there an easier way? I got help on a similar problem, but it dealt with different data frames of very similar names. Here is that code:
CrimeAll <- do.call(
rbind,
lapply(
2018:2022, (yr)
within(get(paste0("Crime", yr)), { year <- yr })
)
)
# ASSIGN AND SUBSET COUNTIES IN ALL 5 YEARS
CrimeAll <- within(
CrimeAll, { county_n <- ave(year, FIPS, FUN=length) }
) |> subset(
subset = county_n == 5, select = -county_n
)
# BUILD LIST OF YEAR SPLIT DATA FRAMES
CrimeList <- split(CrimeAll, CrimeAll$year)
CrimeList$`2018`
CrimeList$`2019`
...
CrimeList$`2022`
Can I do the same with data frames with non-similar names (i.e. “Crime2018”, “Gini”, “S1901Income2018”, and “S1903Income2018”?