如何从矩阵中提取附近的位置?
How to extract close-by locations from a matrix?
我在 R 中得到了一个包含以公里为单位的距离的矩阵:
structure(c(0, 0.299178017189202, 0.472092562775104, 31.0627244156583,
68.5367247200762, 138.470931004348, 0.299178017189202, 0, 0.298855070662631,
31.1130225293117, 68.70216465045, 138.511349341572, 0.472092562775104,
0.298855070662631, 0, 31.41104035093, 68.9840966568129, 138.808325566886,
31.0627244156583, 31.1130225293117, 31.41104035093, 0, 42.1218726943979,
107.425908015876, 68.5367247200762, 68.70216465045, 68.9840966568129,
42.1218726943979, 0, 82.3261005757895, 138.470931004348, 138.511349341572,
138.808325566886, 107.425908015876, 82.3261005757895, 0), .Dim = c(6L,
6L), .Dimnames = list(c("260100023-01", "260100023-05", "260100034-01",
"260100034-02", "260100238-00", "260201036-00"), c("260100023-01",
"260100023-05", "260100034-01", "260100034-02", "260100238-00",
"260201036-00")))
ID 代表不同的位置。现在,我想创建一个列表,为每个 ID 显示 40 公里半径内的其他 ID。我想这样做的关键是“if”函数。但是我不知道如何在矩阵的每个单元格上逐行应用它。
您可以从单个列中提取元素,其中距离最多为 40,如下所示:
col <- data[1, ]
col[col < 40]
## 260100023-01 260100023-05 260100034-01 260100034-02
## 0.0000000 0.2991780 0.4720926 31.0627244
如您所见,生成的矢量的名称恰好是 40 公里以内的位置的 ID。使用 names()
,我们可以提取
ID:
names(col[col < 40])
## [1] "260100023-01" "260100023-05" "260100034-01" "260100034-02"
现在我们知道如何对单个列执行此操作,我们可以对所有列使用 apply()
到 运行 此函数
apply(data, 2, function(col) names(col[col > 40]))
## $`260100023-01`
## [1] "260100238-00" "260201036-00"
##
## $`260100023-05`
## [1] "260100238-00" "260201036-00"
##
## $`260100034-01`
## [1] "260100238-00" "260201036-00"
##
## $`260100034-02`
## [1] "260100238-00" "260201036-00"
##
## $`260100238-00`
## [1] "260100023-01" "260100023-05" "260100034-01" "260100034-02" "260201036-00"
##
## $`260201036-00`
## [1] "260100023-01" "260100023-05" "260100034-01" "260100034-02" "260100238-00"
我在 R 中得到了一个包含以公里为单位的距离的矩阵:
structure(c(0, 0.299178017189202, 0.472092562775104, 31.0627244156583,
68.5367247200762, 138.470931004348, 0.299178017189202, 0, 0.298855070662631,
31.1130225293117, 68.70216465045, 138.511349341572, 0.472092562775104,
0.298855070662631, 0, 31.41104035093, 68.9840966568129, 138.808325566886,
31.0627244156583, 31.1130225293117, 31.41104035093, 0, 42.1218726943979,
107.425908015876, 68.5367247200762, 68.70216465045, 68.9840966568129,
42.1218726943979, 0, 82.3261005757895, 138.470931004348, 138.511349341572,
138.808325566886, 107.425908015876, 82.3261005757895, 0), .Dim = c(6L,
6L), .Dimnames = list(c("260100023-01", "260100023-05", "260100034-01",
"260100034-02", "260100238-00", "260201036-00"), c("260100023-01",
"260100023-05", "260100034-01", "260100034-02", "260100238-00",
"260201036-00")))
ID 代表不同的位置。现在,我想创建一个列表,为每个 ID 显示 40 公里半径内的其他 ID。我想这样做的关键是“if”函数。但是我不知道如何在矩阵的每个单元格上逐行应用它。
您可以从单个列中提取元素,其中距离最多为 40,如下所示:
col <- data[1, ]
col[col < 40]
## 260100023-01 260100023-05 260100034-01 260100034-02
## 0.0000000 0.2991780 0.4720926 31.0627244
如您所见,生成的矢量的名称恰好是 40 公里以内的位置的 ID。使用 names()
,我们可以提取
ID:
names(col[col < 40])
## [1] "260100023-01" "260100023-05" "260100034-01" "260100034-02"
现在我们知道如何对单个列执行此操作,我们可以对所有列使用 apply()
到 运行 此函数
apply(data, 2, function(col) names(col[col > 40]))
## $`260100023-01`
## [1] "260100238-00" "260201036-00"
##
## $`260100023-05`
## [1] "260100238-00" "260201036-00"
##
## $`260100034-01`
## [1] "260100238-00" "260201036-00"
##
## $`260100034-02`
## [1] "260100238-00" "260201036-00"
##
## $`260100238-00`
## [1] "260100023-01" "260100023-05" "260100034-01" "260100034-02" "260201036-00"
##
## $`260201036-00`
## [1] "260100023-01" "260100023-05" "260100034-01" "260100034-02" "260100238-00"