【问题标题】:R extract() doesn´t accept my coordinatesR extract() 不接受我的坐标
【发布时间】:2018-08-25 11:54:25
【问题描述】:

首先,我是编程新手,所以这可能是一个简单的问题,但我无法在任何地方找到解决方案。

我一直在使用此代码从一组堆叠栅格中提取值:

raster.files <- list.files() 
raster.list <- list()
raster.files <-list.files(".",pattern ="asc")

for(i in 1: length(raster.files)){
raster.list[i] <- raster(raster.files[i])}

stacking <- stack(raster.list)
coord <- read.csv2("...")
extract.data <- extract(stacking,coord,method="simple")

到目前为止,我已经多次使用此代码没有任何问题。每次我运行提取行时,都会出现此错误:

Error in .doCellFromXY(object@ncols, object@nrows, object@extent@xmin,  : 
  Not compatible with requested type: [type=character; target=double].

坐标文件包含在具有 2 列(分别为 X 和 Y)的 data.frame 中。

【问题讨论】:

    标签: r coordinates raster


    【解决方案1】:

    我设法找到了绕过此错误的方法,但从技术上讲,它不是解决方案,因为我不明白为什么 R 首先将我的数据视为文本。

    基本上我将 X 列和 Y 列分开并单独处理它们,然后将它们再次绑定到一个新的 data.frame 中:

    coord_matrix_x<-as.numeric(as.matrix(coord[1]))
    coord_matrix_y<-as.numeric(as.matrix(coord[2]))
    coord2 <- cbind(coord_matrix_x, coord_matrix_y)
    coord2<-as.data.frame(coord2)
    coordinates(coord2)<-c("coord_matrix_x","coord_matrix_y")
    

    这是最优雅的方式,但它确实有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多