R:重命名矢量文件中的坐标

R: Rename coordinates in vector file

我想重命名包含点的矢量文件的坐标。

head(pts@coords)

coords.x1 coords.x2
[1,]  960772.6   8784075
[2,]  952244.0   8629062
[3,]  969143.5   8790772
[4,]  994527.3   8798867
[5,]  828560.8   8799644
[6,]  887490.3   8600384

尝试从 spdplyr 库中执行以下操作时:

pts <- pts@coords %>% 
  rename(x = coords.x1,
         y = coords.x2)

我收到错误

Error in UseMethod("rename_") : no applicable method for 'rename_' applied to an object of class "c('matrix', 'array', 'double', 'numeric')"

如何在 R 中重命名这些坐标?

是一个matrix。我们可以转换为 data.frametibble 作为 ?rename 建议

.data - A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details.

library(dplyr)
pts@coords <- pts@coords %>%
    as.data.frame %>%
    rename(x = coords.x1, y = coords.x2) %>%
    as.matrix

base R中,这是一个简单的过程

colnames(pts@coords) <- c('x', 'y')