R 不喜欢我的坐标 - st_as_sf()
R doesn't like my coordinates - st_as_sf()
正在尝试通过此 post () 将数据框转换为 sf 对象。但是我不断收到以下错误..
Error in `[.default`(x, coords) : only 0's may be mixed with negative subscripts
我试图根据这个 post (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,0")
其中 dat2 只是与该纬度和经度相关的风暴事件的坐标和唯一 ID(组 ID)。
更新:我尝试了一些其他的东西,例如:
coordinates <- as.data.frame(cbind(dat2$centroid_lon, dat2$centroid_lat))
dat.sf <- st_as_sf(x=dat2,
coords = coordinates,
crs= "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,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,
coords = coordinates,
crs= "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
并收到此 Error in as.matrix(x)[i] : subscript out of bounds
。 traceback() 告诉我这绝对是我坐标的问题。
如果您查看 st_as_sf
的文档,您会看到 coords
参数应该是:
coords :
in case of point data: names or numbers of the numeric columns holding coordinates
所以您需要传递列的名称,但您传递的是列本身。您还将看到 coords
在您链接的答案中的使用方式。因此,如果您这样做:
dat.sf <- st_as_sf(x=dat2,
coords = c("centroid_lon", "centroid_lat"),
crs= "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
你应该得到正确的结果。
正在尝试通过此 post (Error in `[.default`(x, coords) : only 0's may be mixed with negative subscripts
我试图根据这个 post (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,0")
其中 dat2 只是与该纬度和经度相关的风暴事件的坐标和唯一 ID(组 ID)。
更新:我尝试了一些其他的东西,例如:
coordinates <- as.data.frame(cbind(dat2$centroid_lon, dat2$centroid_lat))
dat.sf <- st_as_sf(x=dat2,
coords = coordinates,
crs= "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,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,
coords = coordinates,
crs= "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
并收到此 Error in as.matrix(x)[i] : subscript out of bounds
。 traceback() 告诉我这绝对是我坐标的问题。
如果您查看 st_as_sf
的文档,您会看到 coords
参数应该是:
coords : in case of point data: names or numbers of the numeric columns holding coordinates
所以您需要传递列的名称,但您传递的是列本身。您还将看到 coords
在您链接的答案中的使用方式。因此,如果您这样做:
dat.sf <- st_as_sf(x=dat2,
coords = c("centroid_lon", "centroid_lat"),
crs= "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
你应该得到正确的结果。