gpt4 book ai didi

R:在 X-Y 网格中查找缺失的组合

转载 作者:行者123 更新时间:2023-12-04 11:36:44 25 4
gpt4 key购买 nike

我有一个由 X 和 Y 坐标形成的大网格,每个网格代表一个值。但是,网格内的某些组合不存在,参见附图:grid with missing combinations

我想用 R 脚本识别缺失的 x-y-组合,但不知道如何做到这一点。获得这些组合的效率是多少?

我的数据示例:

df1 <- structure(list(coord_n = c(1065125L, 1065875L, 1064625L, 1064375L, 
1065625L, 1065375L, 1065625L, 1065125L, 1065625L, 1065125L, 1066125L,
1064625L, 1066375L, 1064125L, 1064375L, 1064625L, 1066375L, 1064875L,
1066125L, 1066625L, 1064375L, 1065125L, 1066375L, 1066625L, 1065125L,
1065875L, 1064125L, 1064375L, 1064125L, 1065875L, 1064625L, 1065125L,
1065125L, 1065625L, 1066375L, 1064375L, 1064875L, 1065875L, 1066375L,
1066625L, 1064375L, 1064625L, 1066375L, 1065875L, 1065375L, 1065375L,
1066625L, 1065375L, 1064625L, 1066625L, 1066125L, 1065625L, 1065375L,
1065875L, 1064125L, 1064375L, 1064875L, 1065625L, 1065625L, 1064625L,
1064875L, 1065375L, 1065875L, 1065875L, 1066625L, 1065875L, 1064875L,
1066625L, 1064875L, 1064125L, 1066125L, 1064375L, 1066375L, 1064125L,
1066625L, 1065125L, 1064625L, 1065625L, 1066125L, 1064125L, 1066375L,
1066625L, 1066375L, 1064125L, 1064875L, 1065375L, 1064375L, 1065625L,
1065875L, 1065375L, 1066375L, 1064875L, 1064375L, 1066625L, 1064375L,
1065875L, 1064375L, 1065375L, 1064875L, 1066375L), coord_e = c(2418625L,
2419125L, 2421875L, 2418125L, 2421375L, 2422375L, 2421125L, 2418875L,
2418625L, 2420375L, 2419375L, 2420625L, 2418875L, 2420625L, 2419125L,
2420875L, 2419125L, 2419875L, 2418375L, 2421625L, 2422375L, 2422375L,
2422125L, 2422125L, 2420125L, 2421875L, 2421875L, 2420125L, 2422375L,
2420625L, 2419625L, 2418375L, 2419625L, 2418375L, 2419875L, 2420875L,
2421375L, 2422375L, 2422375L, 2418125L, 2418375L, 2419125L, 2418625L,
2418875L, 2419375L, 2421375L, 2421125L, 2419125L, 2418375L, 2419625L,
2418875L, 2420125L, 2419875L, 2420375L, 2420375L, 2419875L, 2420375L,
2422375L, 2421875L, 2422375L, 2419375L, 2420875L, 2421125L, 2421375L,
2419125L, 2419375L, 2421625L, 2418375L, 2418875L, 2418375L, 2420125L,
2419625L, 2418375L, 2420125L, 2421375L, 2422125L, 2419875L, 2420375L,
2420375L, 2418625L, 2421125L, 2420125L, 2421625L, 2419875L, 2419125L,
2420625L, 2418625L, 2419375L, 2420125L, 2418125L, 2420125L, 2418625L,
2418875L, 2418625L, 2421125L, 2419875L, 2421375L, 2418875L, 2420875L,
2421875L), density_value = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), percentage_free = c(100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100)), class = c("data.table",
"data.frame"), row.names = c(NA, -100L))

最佳答案

这是使用 dplyr 的另一种方法和 tidyr获取丢失的“像素”:

library(dplyr)
library(tidyr)

df1 %>%
expand(coord_n = seq(min(df1$coord_n), max(df1$coord_n), 250),
coord_e = seq(min(df1$coord_e), max(df1$coord_e), 250)) %>%
anti_join(.,df1)

#> Joining, by = c("coord_n", "coord_e")
#> # A tibble: 98 x 2
#> coord_n coord_e
#> <dbl> <dbl>
#> 1 1064125 2418125
#> 2 1064125 2418875
#> 3 1064125 2419125
#> 4 1064125 2419375
#> 5 1064125 2419625
#> 6 1064125 2420875
#> 7 1064125 2421125
#> 8 1064125 2421375
#> 9 1064125 2421625
#> 10 1064125 2422125
#> # ... with 88 more rows



或引用 Frank's answerdata.table :
missingDT <- function(DT, cols, defs = NULL){
require(data.table)
mDT = do.call(CJ, c(DT[, ..cols], list(unique=TRUE)))
res = DT[mDT, on=names(mDT)]
if (length(defs))
res[, names(defs) := Map(replace, .SD, lapply(.SD, is.na), defs), .SDcols=names(defs)]
res[!complete.cases(res), cols, with=FALSE]
}

missingDT(setDT(df1), cols = c("coord_n", "coord_e"))

# coord_n coord_e
# 1: 1064125 2418125
# 2: 1064125 2418875
# 3: 1064125 2419125
# 4: 1064125 2419375
# 5: 1064125 2419625
# ---
# 94: 1066625 2420375
# 95: 1066625 2420625
# 96: 1066625 2420875
# 97: 1066625 2421875
# 98: 1066625 2422375
# coord_n coord_e

关于R:在 X-Y 网格中查找缺失的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57257383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com