I am trying to pass a column name through the bi_class
function within my broader create_sf_func()
function to use to create tertile cutoffs for bivariate maps in R. I am using the following code, but cannot figure out how to get R to recognize my variable
argument of my function as a variable in the dataframe. Any help would be greatly appreciated.
#LIBRARIES
library(tidyverse)
library(biscale)
library(sf)
library(tigris)
library(stringr)
#Get Sample of FIPS codes
all_fips <- get(data(fips_codes)) %>%
mutate(FIPS5 = paste0(state_code, county_code)) %>%
select(FIPS5) %>%
head(100)
#Create example data
df <- data.frame(all_fips[1], YrQrt = sample(c("2022Q1", "2022Q2"), 100, replace=TRUE, prob=c(0.5, 0.5)),
TestRate=runif(100, 0, 60),
CaseRate=runif(100, 0, 70),
DeathRate=runif(100, 0, 30)) %>%
mutate(FIPS5 = str_pad(FIPS5, width=5, pad="0"))
#Produce tertiles for x and y variables and produce geometry shapes for the FIPS codes
df_cases <- bi_class(df, x = TestRate, y = CaseRate, style = "quantile", dim = 3)
df_cases2022Q2 <- sf::st_as_sf(df_cases %>% filter(YrQrt == "2022Q2") %>%
mutate(geometry = fips_geometry(FIPS5)) %>% filter(FIPS5<60), crs = 4269) %>%
shift_geometry()
This is where I am trying to turn the above into a function because I have a number of variables to perform this on:
#Turn the above tertile and geometry production into a function
create_sf_func <- function(yrqrt, variable) {
df2 <- bi_class(df, x = TestRate, y = ensym(variable), style = "quantile", dim = 3)
df_filtered <- sf::st_as_sf(df2 %>% filter(YrQrt== yrqrt) %>%
right_join(all_fips, by=c("FIPS5"="FIPS5")) %>%
mutate(geometry = fips_geometry(FIPS5)) %>% filter(FIPS5<60), crs = 4269) %>%
shift_geometry()
return(df_filtered)
}
#run function
df_cases2022Q2 <- create_sf_func("2022Q2", "CaseRate")
CodePudding user response:
I already had a look at your previous question and the only option which worked for me was to use do.call
to call bi_class
and pass the variable name:
library(tidyverse)
library(biscale)
library(sf)
library(tigris)
library(fipio)
set.seed(123)
create_sf_func <- function(yrqrt, variable) {
df2 <- do.call(bi_class, list(df, x = "TestRate", y = variable, style = "quantile", dim = 3))
df_filtered <- sf::st_as_sf(df2 %>% filter(YrQrt == yrqrt) %>%
right_join(all_fips, by = c("FIPS5" = "FIPS5")) %>%
mutate(geometry = fips_geometry(FIPS5)) %>% filter(FIPS5 < 60), crs = 4269) %>%
shift_geometry()
return(df_filtered)
}
df_cases2022Q2 <- create_sf_func("2022Q2", "CaseRate")
#> Warning: st_crs<- : replacing crs does not reproject data; use st_transform for
#> that
df_cases2022Q2
#> Simple feature collection with 100 features and 6 fields (with 5 geometries empty)
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -3115585 ymin: -1702302 xmax: 1044909 ymax: -230281.9
#> Projected CRS: USA_Contiguous_Albers_Equal_Area_Conic
#> First 10 features:
#> FIPS5 YrQrt TestRate CaseRate DeathRate bi_class
#> 1 01001 2022Q2 35.999338 16.710822 23.537258 2-1
#> 2 01005 2022Q2 29.316782 42.095601 23.371976 2-2
#> 3 01011 2022Q2 53.421013 61.617258 14.427325 3-3
#> 4 01019 2022Q2 8.825681 12.052022 14.768800 1-1
#> 5 01023 2022Q2 18.073734 17.707545 13.939978 1-1
#> 6 01029 2022Q2 43.235776 3.336454 10.643493 3-1
#> 7 01033 2022Q2 32.957079 24.632205 25.071265 2-2
#> 8 01035 2022Q2 57.245474 28.626080 7.132482 3-2
#> 9 01037 2022Q2 35.129001 57.466593 10.619583 2-3
#> 10 01057 2022Q2 8.514414 48.878312 10.151739 1-3
#> geometry
#> 1 MULTIPOLYGON (((858778.6 -5...
#> 2 MULTIPOLYGON (((997958.5 -6...
#> 3 MULTIPOLYGON (((972148.2 -5...
#> 4 MULTIPOLYGON (((953138.5 -2...
#> 5 MULTIPOLYGON (((745962.9 -6...
#> 6 MULTIPOLYGON (((953876.1 -3...
#> 7 MULTIPOLYGON (((777011.4 -2...
#> 8 MULTIPOLYGON (((881959.9 -6...
#> 9 MULTIPOLYGON (((929183.3 -4...
#> 10 MULTIPOLYGON (((766776.4 -3...