Use purrr () instead of binding to arbitrary city / county parades in tidycensus?

I have a giant data file upload application. But it seems awkward. But mapply doesn't seem right as I don't need all state / county combinations. I am good at map (). Can anyone provide an example of how I can use the purrr () "map" command for the following code?

library(tidycensus)
library(sf)
mykey<-"youhavetogetyourownimafraid"
#variables to test out the function#############
x<-"06"
y<-"073"
z<-"2000"
setwd("N:/Dropbox/_BonesFirst/149_Transit_Metros_BG_StateSplit_by_R")
##################now the actual function#########################
get_Census <- function(x,y,z) {
  name<-paste0("transitmetro_",x,"_",y,"_",z)
  name
  namefile<-get_decennial(geography = "block group",
                variables = "P001001",
                sumfile = "sf1",
                key = mykey,
                state = x, county = y,year = z,
                geometry = TRUE)
  st_write(namefile, paste0(name,".shp")) #tidycensus version of write OGR
}  

#now, for all of them
CO<-c("013")
tibble04_10<-lapply(CO,get_Census,x="04",z="2000")
CO<-c("067","073","113")
tibble06_10<-lapply(CO,get_Census,x="06",z="2000")
CO<-c("005","031","035")
tibble08_10<-lapply(CO,get_Census,x="08",z="2000")
CO<-c("037","053","123")
tibble27_10<-lapply(CO,get_Census,x="27",z="2000")
CO<-c("119")
tibble37_10<-lapply(CO,get_Census,x="37",z="2000")
CO<-c("085","113","121","201")
tibble48_10<-lapply(CO,get_Census,x="48",z="2000")
CO<-c("035") #SLCO, utah
tibble49_10<-lapply(CO,get_Census,x="49",z="2000")
CO<-c("033","053") #King co, Seattle
tibble53_10<-lapply(CO,get_Census,x="53",z="2000")

      

EDIT

get_Census <- function(x,y,z) {
  name<-paste0("transitmetro_",x,"_",y,"_",z)
  name
  namefile<-get_decennial(geography = "block group",
                          variables = "P001001",
                          sumfile = "sf1",
                          key = mykey,
                          state = x, county = y,year = z,
                          geometry = TRUE)
  st_write(namefile, paste0(name,".shp")) #tidycensus version of write OGR
}  

CO_list <- list(c("013"),
                c("067","073","113"),
                c("005","031","035"),
                c("037","053","123"),
                c("119"),
                c("085","113","121","201"),
                c("035"),
                c("033","053"))

x_list <- c("04", "06", "08", "27", "37", "48", "49", "53")
z_list <- c("2000", "2000", "2000", "2000", "2000", "2000", "2000", "2000")
# BUILD LIST OF OBJECTS
tibble_list <- Map(function(CO, x, z) lapply(CO, function(i) get_Census(i, x, z)), 
                   CO_list, x_list, z_list)
# NAME LIST OF OBJECTS: tibble04_10, tibble06_10, tibble08_10, ...
tibble_list <- setNames(tibble_list, paste0("tibble", x_list, "_10"))
print(tibble_list)

      

Productivity:

Retrieving data from the 2000 Decadal Census Error: Outcome 1 is not a 1st atomic vector Also: Warning Messages: 1: '004' is not a valid FIPS code for counties in Georgia
2: '004' is not a valid FIPS code for counties in Georgia
Show Traceback
Retry with Debug Error in gather_ (data, key_col = compat_as_lazy (enquo (key)), value_col = compat_as_lazy (enquo (value)),: unused argument (-NAME)

+2


source to share


2 answers


Let's revisit the base R family, namely Map

(wrapper to mapply

) and lapply

, which I hear well about. Just create lists of equal length to go into a nested function call.

CO_list <- list(c("013"),
                c("067","073","113"),
                c("005","031","035"),
                c("037","053","123"),
                c("119"),
                c("085","113","121","201"),
                c("035"),
                c("033","053"))

x_list <- c("04", "06", "08", "27", "37", "48", "49", "53")
z_list <- c("2000", "2000", "2000", "2000", "2000", "2000", "2000", "2000")

# BUILD LIST OF OBJECTS
tibble_list <- Map(function(CO, x, z) lapply(CO, function(i) get_Census(i, x, z)), 
                   CO_list, x_list, z_list)

# NAME LIST OF OBJECTS: tibble04_10, tibble06_10, tibble08_10, ...
tibble_list <- setNames(tibble_list, paste0("tibble", x_list, "_10"))

      



Also, since z_list is redundant, you can still shorten:

tibble_list <- Map(function(CO, x) lapply(CO, function(i) get_Census(i, x, z=2000)), 
                   CO_list, x_list)

      

+3


source


You can use map

from purrr

to map a list of your county characteristics. One way to do this is with a list of lists, for example:

fips_yrs <- list(
    sl_co = list(x = "49", y = "035", z = 2000),
    king_co = list(x = "53", y = "053", z = 2000)
)

      

It map

will then display for each county, and you can display its information by name with [[1]]

.



map(fips_yrs, ~get_Census(x = .$x[[1]], y = .$y[[1]], z = .$z[[1]]))

      

FYI, if all you want is shapefiles, it tidycensus

uses functions from tigris

to load its shapefiles, so you can just call the function tigris

.

+2


source







All Articles