r语言 - 在函数上使用Map如果df列表的名称匹配



我给了一个

a namend vector col:

col <- c(id = "CLUSTER", x = "LONGNUM", y = "LATNUM", n = "Severely.stunted.child.under.5.years..Total", pos = "Severely.stunted.child.under.5.years.Yes")

#a列元素为col的数据框列表,以特定的研究区域命名。(见下文)列表结果包含19个不同的文件(不同年份)

一个包含6个元素(对应国家)的shapefiles列表:

study_area <- c("Ethiopia", "Liberia", "Malawi", "Rwanda", "Uganda", "Zimbabwe")
Countries <- lapply(study_area, function(x){gisco_get_countries(country=  x, resolution = 60 )})
Countries <- lapply(Countries, function(x) {as_Spatial(x, cast = TRUE, IDs = c("CNTR_NAME", "ISO§_CODE", "CNRT_ID", "NAME_ENGL", "FID"))})
names(Countries) <- study_area

我想从prevR库中执行函数:

s.prevR(Results[[1]], col, Countries[[1]])

但实际上对于列表中每个名称合适的元素:

我尝试了这样做:Map(function(x, y) { as.prevR(x, col, y)}, Results, Countries)

但是这里(显然)不匹配x和y的名字

dput( dput(Results[[1]][1:5,1:24])
structure(list(CLUSTER = c("", "1", "10", "100", "101"), Severely.stunted.child.under.5.years.No = c(3438, 
8, 7, 9, 6), Severely.stunted.child.under.5.years.Yes = c(1047, 
4, NA, 7, 1), Severely.stunted.child.under.5.years..Total = c(4485, 
12, 7, 16, 7), Stunted.child.under.5.years.No = c(2531, 2, 7, 
7, 5), Stunted.child.under.5.years.Yes = c(1954, 10, NA, 9, 2
), Stunted.child.under.5.years..Total = c(4485, 12, 7, 16, 7), 
Severely.wasted.child.under.5.years.No = c(4295, 11, 7, 16, 
7), Severely.wasted.child.under.5.years.Yes = c(190, 1, NA, 
NA, NA), Severely.wasted.child.under.5.years..Total = c(4485, 
12, 7, 16, 7), Wasted.child.under.5.years.No = c(3957, 10, 
7, 16, 6), Wasted.child.under.5.years.Yes = c(528, 2, NA, 
NA, 1), Wasted.child.under.5.years..Total = c(4485, 12, 7, 
16, 7), Severely.underweight.child.under.5.years.No = c(4028, 
10, 7, 12, 7), Severely.underweight.child.under.5.years.Yes = c(457, 
2, NA, 4, NA), Severely.underweight.child.under.5.years..Total = c(4485, 
12, 7, 16, 7), Underweight.child.under.5.years.No = c(3185, 
7, 7, 12, 5), Underweight.child.under.5.years.Yes = c(1300, 
5, NA, 4, 2), Underweight.child.under.5.years..Total = c(4485, 
12, 7, 16, 7), LATNUM = c(NA, 10.889096, 5.323272, 8.830199, 
10.806748), LONGNUM = c(NA, 37.269565, 39.556812, 40.72964, 
39.7703), SurveyId = c("ET2005DHS", "ET2005DHS", "ET2005DHS", 
"ET2005DHS", "ET2005DHS"), DHSC = c("ET", "ET", "ET", "ET", 
"ET"), Country = c("Ethiopia", "Ethiopia", "Ethiopia", "Ethiopia", 
"Ethiopia")), row.names = c(NA, 5L), class = "data.frame")

国家和

dput(Countries[[1]])
new("SpatialPolygonsDataFrame", data = structure(list(CNTR_NAME = "Federal Democratic Republic of Ethiopia", 
ISO3_CODE = "ETH", CNTR_ID = "ET", NAME_ENGL = "Ethiopia", 
FID = "ET"), class = "data.frame", row.names = 1L), polygons = list(
new("Polygons", Polygons = list(new("Polygon", labpt = c(39.6420582930584, 
8.63562315843106), area = 93.13026982, hole = FALSE, ringDir = 1L, 
coords = structure(c(41.6307, 42.4043, 41.816, 41.8348, 
42.9681, 42.7628, 42.9804, 43.9589, 45.6126, 46.9411, 
47.8524, 45.6126, 45.4747, 45.2923, 44.9162, 43.4741, 
42.8138, 41.9101, 41.2328, 40.708, 39.9305, 39.5667, 
38.9731, 38.1026, 36.9621, 35.9477, 35.8294, 35.3235, 
35.0325, 34.9588, 34.5428, 33.7557, 33.0448, 33.2485, 
33.8204, 34.0937, 34.1132, 34.4181, 34.8021, 35.2153, 
35.6227, 36.1342, 36.5603, 37.2972, 37.5268, 37.9201, 
38.5391, 39.0217, 40.0851, 40.8941, 41.6307, 13.3913, 
12.4686, 11.6292, 11.0448, 10.9974, 10.7159, 10.0644, 
9.0545, 8.4674, 8.0224, 7.9151, 5.5657, 5.4241, 5.2367, 
4.9368, 4.7993, 4.301, 3.9823, 3.9616, 4.2326, 3.8858, 
3.5224, 3.5158, 3.6459, 4.3833, 4.62, 5.2367, 5.413, 
5.8494, 6.4537, 6.7418, 7.6074, 7.899, 8.381, 8.4168, 
8.6026, 9.4986, 10.6735, 10.8052, 11.9187, 12.5064, 12.8315, 
14.2577, 14.3876, 14.2588, 14.8128, 14.4413, 14.5899, 
14.5456, 14.0891, 13.3913), dim = c(51L, 2L)))), plotOrder = 1L, 
labpt = c(39.6420582930584, 8.63562315843106), ID = "1", 
area = 93.13026982)), plotOrder = 1L, bbox = structure(c(33.0448, 
3.5158, 47.8524, 14.8128), dim = c(2L, 2L), dimnames = list(c("x", 
"y"), c("min", "max"))), proj4string = new("CRS", projargs = "+proj=longlat +datum=WGS84 +no_defs"))

如果Countries名称都在Results名称中并且如果'Results'有重复的名称,那么我们可以通过基于'Results'的名称复制来使Countries具有相同的长度

Map(function(x, y) { as.prevR(x, col, y)}, Results, Countries[names(Results)])

相关内容

  • 没有找到相关文章

最新更新