如何在R中的sf对象中应用条件?

发布于 2025-01-18 12:20:27 字数 207 浏览 3 评论 0原文

Assuming this data here

library(sf)
fname <- system.file("shape/nc.shp", package="sf")
nc <- st_read(fname)

如果在几何中 lat 是 > 32和< 33、然后将归档的NAME中所有对应的“Ashe”重命名为“new”

Assuming this data here

library(sf)
fname <- system.file("shape/nc.shp", package="sf")
nc <- st_read(fname)

If in geomtry lat is > 32 and < 33, then rename all corresponding "Ashe" in the filed NAME to "new"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紧拥背影 2025-01-25 12:20:27

我认为,如果我正确理解您,那么您想根据几何列中存在的坐标选择SF数据框架的行。在32和33度之间的特定数据框架中没有对象,正如我们可以看到以下图:

ggplot(nc) + geom_sf()

“在此处输入图像说明”

这样,让我们​​选择几个几何形状完全在35和36范围内的行,

in_range <- vapply(nc$geometry, function(x) {
  lat <- as.matrix(x)[,2]
  all(lat > 35 & lat < 36)
}, logical(1))

nc$NAME[in_range] <- "new"

ggplot(nc, aes(fill = NAME == "new")) + geom_sf()


编辑

根据几何形状是否在范围以及其他条件下更改name,您可以将nc视为一个正常R数据框。

library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
fname <- system.file("shape/nc.shp", package="sf")
nc <- st_read(fname)

in_range <- vapply(nc$geometry, function(x) {
  lat <- as.matrix(x)[,2]
  all(lat > 35 & lat < 36)
}, logical(1))

nc$NAME[in_range & nc$NAME == "McDowell"] <- "new"

因此,现在几何形状处于范围内的任何行,名称是“ McDowell”的 name 更改为“ new”。我们可以像这样说明:

nc[in_range,]
#> Simple feature collection with 35 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.03086 ymin: 35.00028 xmax: -75.7637 ymax: 35.99465
#> Geodetic CRS:  NAD27
#> First 10 features:
#>     AREA PERIMETER CNTY_ CNTY_ID       NAME  FIPS FIPSNO CRESS_ID BIR74 SID74
#> 43 0.134     1.755  1958    1958      Burke 37023  37023       12  3573     5
#> 44 0.100     1.331  1962    1962 Washington 37187  37187       94   990     5
#> 45 0.099     1.411  1963    1963    Tyrrell 37177  37177       89   248     0
#> 46 0.116     1.664  1964    1964        new 37111  37111       56  1946     5
#> 47 0.201     1.805  1968    1968   Randolph 37151  37151       76  4456     7
#> 48 0.180     2.142  1973    1973    Chatham 37037  37037       19  1646     2
#> 49 0.094     1.307  1979    1979     Wilson 37195  37195       98  3702    11
#> 50 0.134     1.590  1980    1980      Rowan 37159  37159       80  4606     3
#> 51 0.168     1.791  1984    1984       Pitt 37147  37147       74  5094    14
#> 52 0.106     1.444  1986    1986    Catawba 37035  37035       18  5754     5
#>    NWBIR74 BIR79 SID79 NWBIR79                       geometry
#> 43     326  4314    15     407 MULTIPOLYGON (((-81.81628 3...
#> 44     521  1141     0     651 MULTIPOLYGON (((-76.40843 3...
#> 45     116   319     0     141 MULTIPOLYGON (((-76.1673 35...
#> 46     134  2215     5     128 MULTIPOLYGON (((-81.81628 3...
#> 47     384  5711    12     483 MULTIPOLYGON (((-79.76499 3...
#> 48     591  2398     3     687 MULTIPOLYGON (((-79.55536 3...
#> 49    1827  4706    13    2330 MULTIPOLYGON (((-78.06533 3...
#> 50    1057  6427     8    1504 MULTIPOLYGON (((-80.29824 3...
#> 51    2620  6635    11    3059 MULTIPOLYGON (((-77.47388 3...
#> 52     790  6883    21     914 MULTIPOLYGON (((-80.96143 3...

reprex package (v2)。 0.1)

I think, if I understand you correctly, then you want to select rows of the sf data frame based on the co-ordinates present in the geometry column. There are no objects in that particular data frame between 32 and 33 degrees, as we can see with the following plot:

ggplot(nc) + geom_sf()

enter image description here

So let's select rows where the geometry is entirely within the range 35 and 36.

in_range <- vapply(nc$geometry, function(x) {
  lat <- as.matrix(x)[,2]
  all(lat > 35 & lat < 36)
}, logical(1))

nc$NAME[in_range] <- "new"

ggplot(nc, aes(fill = NAME == "new")) + geom_sf()

enter image description here


Edit

To change the NAME according to whether the geometry is in range plus other conditions, you can treat nc as a normal R data frame.

library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
fname <- system.file("shape/nc.shp", package="sf")
nc <- st_read(fname)

in_range <- vapply(nc$geometry, function(x) {
  lat <- as.matrix(x)[,2]
  all(lat > 35 & lat < 36)
}, logical(1))

nc$NAME[in_range & nc$NAME == "McDowell"] <- "new"

So now any rows where the geometry was in range and NAME was "McDowell" have hsd their NAME changed to "new". We can demonstrate this like so:

nc[in_range,]
#> Simple feature collection with 35 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.03086 ymin: 35.00028 xmax: -75.7637 ymax: 35.99465
#> Geodetic CRS:  NAD27
#> First 10 features:
#>     AREA PERIMETER CNTY_ CNTY_ID       NAME  FIPS FIPSNO CRESS_ID BIR74 SID74
#> 43 0.134     1.755  1958    1958      Burke 37023  37023       12  3573     5
#> 44 0.100     1.331  1962    1962 Washington 37187  37187       94   990     5
#> 45 0.099     1.411  1963    1963    Tyrrell 37177  37177       89   248     0
#> 46 0.116     1.664  1964    1964        new 37111  37111       56  1946     5
#> 47 0.201     1.805  1968    1968   Randolph 37151  37151       76  4456     7
#> 48 0.180     2.142  1973    1973    Chatham 37037  37037       19  1646     2
#> 49 0.094     1.307  1979    1979     Wilson 37195  37195       98  3702    11
#> 50 0.134     1.590  1980    1980      Rowan 37159  37159       80  4606     3
#> 51 0.168     1.791  1984    1984       Pitt 37147  37147       74  5094    14
#> 52 0.106     1.444  1986    1986    Catawba 37035  37035       18  5754     5
#>    NWBIR74 BIR79 SID79 NWBIR79                       geometry
#> 43     326  4314    15     407 MULTIPOLYGON (((-81.81628 3...
#> 44     521  1141     0     651 MULTIPOLYGON (((-76.40843 3...
#> 45     116   319     0     141 MULTIPOLYGON (((-76.1673 35...
#> 46     134  2215     5     128 MULTIPOLYGON (((-81.81628 3...
#> 47     384  5711    12     483 MULTIPOLYGON (((-79.76499 3...
#> 48     591  2398     3     687 MULTIPOLYGON (((-79.55536 3...
#> 49    1827  4706    13    2330 MULTIPOLYGON (((-78.06533 3...
#> 50    1057  6427     8    1504 MULTIPOLYGON (((-80.29824 3...
#> 51    2620  6635    11    3059 MULTIPOLYGON (((-77.47388 3...
#> 52     790  6883    21     914 MULTIPOLYGON (((-80.96143 3...

Created on 2022-04-02 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文