Joehat
Joehat

Reputation: 1129

R: Rename coordinates in vector file

I'd like to rename the coordinates of a vector file containing points.

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

When trying the following from the spdplyr library:

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

I get the error

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

How could I rename these coordinates in R?

Upvotes: 1

Views: 772

Answers (1)

akrun
akrun

Reputation: 887821

It is a matrix. We can convert to data.frame or tibble as the ?rename suggests

.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

In base R, it is a straightforward process

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

Upvotes: 2

Related Questions