2

我有一个巨大的 lapply 正在运行以下载数据文件。但这似乎很笨拙。但是 mapply 似乎不对,因为我不想要所有的州/县组合。我听说了关于 map() 的好消息。谁能提供一个示例,说明我如何将 purrr() 命令“映射”用于以下代码?

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")

编辑

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)

产量:

从 2000 年十年一次的人口普查中获取数据 错误:结果 1 不是长度为 1 的原子向量此外:警告消息:1:“004”不是格鲁吉亚县的有效 FIPS 代码
2:“004”不是有效的 FIPS 代码对于乔治亚州的县
显示 Traceback
Rerun with Debug Error in gather_(data, key_col = compat_as_lazy(enquo(key)), value_col = compat_as_lazy(enquo(value)), : 未使用的参数 (-NAME)

4

2 回答 2

3

重新考虑基本 R 的应用系列,即Map(wrapper to mapply) 并且lapply我听到了一些好消息。只需构建等长列表以传递给嵌套函数调用。

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"))

此外,由于z_list都是多余的,因此您可以缩短:

tibble_list <- Map(function(CO, x) lapply(CO, function(i) get_Census(i, x, z=2000)), 
                   CO_list, x_list)
于 2018-05-10T18:21:36.993 回答
2

您可以使用mapfrompurrr映射您所在县的特征列表。一种方法是列出列表,例如:

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

然后map将绘制每个县的地图,您可以按名称提取其信息[[1]]

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

仅供参考,如果您只需要 shapefile,则tidycensus使用函数从tigris下载其 shapefile,因此您可以直接调用tigris函数。

于 2018-05-10T17:53:28.320 回答