R不喜欢我的坐标-st_as_sf

问题描述

尝试通过本文(How to Convert data frame to spatial coordinates)将数据帧转换为sf对象。但是我一直收到以下错误。 Error in `[.default`(x,coords) : only 0's may be mixed with negative subscripts

我根据这篇文章(R debugging: "only 0's may be mixed with negative subscripts")尝试解决此问题,因为我的坐标是全局坐标,R不喜欢值的范围。但是,我将其子集仅包含在北半球,并收到了相同的错误。下面是我正在使用的代码

dat.sf <- st_as_sf(x=dat2,coords = c(dat2$centroid_lon,dat2$centroid_lat),crs= "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0")

其中dat2只是坐标和与该经纬度相关的风暴事件的唯一ID(组ID)。

更新:我尝试了其他一些事情,例如:

coordinates <- as.data.frame(cbind(dat2$centroid_lon,dat2$centroid_lat))
dat.sf <- st_as_sf(x=dat2,coords = coordinates,0")

并收到了此Error in `[.default`(x,coords) : invalid subscript type 'list'

还有这个......

coordinates <- cbind(dat2$centroid_lon,dat2$centroid_lat)
dat.sf <- st_as_sf(x=dat2,0")

并收到了此Error in as.matrix(x)[i] : subscript out of bounds。 traceback()告诉我,这绝对是我的坐标问题。

解决方法

如果您查看st_as_sf的文档,则会看到coords参数应为:

坐标: 如果是点数据:保存坐标的数字列的名称或数字

因此您需要传递列名,但是您要传递列本身。您还将看到这是在链接的答案中使用coords的方式。因此,如果您这样做:

dat.sf <- st_as_sf(x=dat2,coords = c("centroid_lon","centroid_lat"),crs= "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0")

您应该得到正确的结果。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...