I have retrieved the rnaturalearth data as sf class, and I am trying to rename one of the columns from "iso_a2" to "countryCode" to more easily join it with another dataset of mine. When I try to rename it using rename(), I am left with this error:
library(rnaturalearth)
library(dplyr)
baseBorderMap <-
ne_countries(returnclass = 'sf')
baseBorderMap <- rename(baseBorderMap, "countryCode" = "iso_a2")
Error in rename.sf(baseBorderMap, countryCode = "iso_a2") : internal error: can't find
agrcolumns`
I was hoping to be able to join these two datasets through a renamed column, but because I am getting an error when trying to rename a column I know exists, I am unable to.
Why am I getting this error when I know that there is a column in the rnaturalearth data named "iso_a2"?
It's a known bug with renaming columns in some sf objects. And that
rnaturalearthdataset has more than a few 'foibles' to contend with. A workaround is toselect()all the columns first: