如何在R中的sf对象中应用条件?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为,如果我正确理解您,那么您想根据几何列中存在的坐标选择SF数据框架的行。在32和33度之间的特定数据框架中没有对象,正如我们可以看到以下图:
这样,让我们选择几个几何形状完全在35和36范围内的行,
编辑
根据几何形状是否在范围以及其他条件下更改
name
,您可以将nc
视为一个正常R数据框。因此,现在几何形状处于范围内的任何行,
名称
是“ McDowell”的 name 更改为“ new”。我们可以像这样说明:由 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:
So let's select rows where the geometry is entirely within the range 35 and 36.
Edit
To change the
NAME
according to whether the geometry is in range plus other conditions, you can treatnc
as a normal R data frame.So now any rows where the geometry was in range and
NAME
was "McDowell" have hsd theirNAME
changed to "new". We can demonstrate this like so:Created on 2022-04-02 by the reprex package (v2.0.1)