Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何基于R中的相邻多边形填充缺少的值?_R_Purrr_Sf - Fatal编程技术网

如何基于R中的相邻多边形填充缺少的值?

如何基于R中的相邻多边形填充缺少的值?,r,purrr,sf,R,Purrr,Sf,假设以下数据集并添加一些缺少的值(仅用于说明): 这给了我一个每行相邻县的指数列表。现在我想用相邻多边形的平均面积来填充缺少的面积值。我将如何使用这些索引获取对应行的平均值 index <- st_touches(nc, nc) output <- nc %>% mutate(AREA = ifelse(is.na(AREA), apply(index, 1, function(i){mean(.$AREA[i]

假设以下数据集并添加一些缺少的值(仅用于说明):

这给了我一个每行相邻县的指数列表。现在我想用相邻多边形的平均面积来填充缺少的面积值。我将如何使用这些索引获取对应行的平均值

index <- st_touches(nc, nc)

output <- nc %>% 
  mutate(AREA = ifelse(is.na(AREA),
                             apply(index, 1, function(i){mean(.$AREA[i])}),
                             AREA))

output$AREA[c(30, 45)]

[1] 0.1510 0.1335
手动查找区域

mean(output$AREA[index[[30]]])

[1] 0.151

mean(output$AREA[index[[45]]])

[1] 0.1335

nc看起来像什么?
index <- st_touches(nc, nc)

output <- nc %>% 
  mutate(AREA = ifelse(is.na(AREA),
                             apply(index, 1, function(i){mean(.$AREA[i])}),
                             AREA))

output$AREA[c(30, 45)]

[1] 0.1510 0.1335
index[c(30, 45)]

[[1]]
[1] 13 14 29 37 48

[[2]]
[1] 44 87
mean(output$AREA[index[[30]]])

[1] 0.151

mean(output$AREA[index[[45]]])

[1] 0.1335