我无法在公开数据中重现此问题,因为这似乎是一个特定于数据的问题。
我有 2 个 sf 对象,在 here 显示为灰色(2020)和黑色(1970)边框。两者都经过
st_make_valid()
,并且 st_is_valid()
对于所有多边形都返回 TRUE。尺寸如下所示:
dim(soilshape.sf.drop) # the 2020 polygon with soil data is 693 11
dim(dist1970.sf) # the 1970 polygon is 386 3
length(dist1970.sf$ID) # the length of the ID is obviously 386
length(unique(dist1970.sf$ID)) # also 386 -- a unique ID
我想运行以下代码,我理解该代码应该在对象 atest1 中创建一个新 ID,该对象保存来自 dist1970.sf 的唯一多边形 id ID:
atest1 = st_interpolate_aw(soilshape.sf["Zn_s"], dist1970.sf, extensive = FALSE) |>
mutate(ID = dist1970.sf$ID)
但是,它不返回任何对象,只返回此错误:
Error in `stopifnot()`:
ℹ In argument: `ID = dist1970.sf$ID`.
Caused by error:
! `ID` must be size 385 or 1, not 386.
Run `rlang::last_trace()` to see where the error occurred.
我认为这是因为,当我不通过 mutate() 进行管道传输时,创建的输出没有 386 个多边形 - 它有 385 个。也就是说,当我运行此代码时:
atest2 = st_interpolate_aw(soilshape.sf["Zn_s"], dist1970.sf, extensive = FALSE)
我得到一个尺寸为 385 x 2 的对象 atest2,而不是预期的 386 x 2。
任何人都可以推测发生了什么 - 为什么 st_interpolate_aw() 创建的对象比 dist1970.sf 中的数字少 1 个多边形/为什么我无法使用 mutate() 管道通过来带来我的 dist1970 .sf ID 与我有关吗?以及如何修复它?插值数据目前无法使用,因为我无法将其与原始 ID 进行匹配。
我找到答案了!我需要包含 keep_NA = TRUE 选项,以在数据提取和维护的 ID 中保留缺失值。所以现在可以了:
atest = st_interpolate_aw(soilshape.sf["Zn_s"], dist1970.sf, extensive = FALSE, keep_NA = TRUE) |>
mutate(ID = dist1970.sf$ID)
也就是说,
atest
与dist1970.sf
的长度相同,并且包含来自dist1970.sf
的ID字段。