How do I rename entries in a specific column of a SpatialPolygonsDataFrame in R?

784 Views Asked by At

I have a SpatialPolygonDataFrame loaded in R. There is a specific column with entries I want to rename to correct typos.

Data$Material has the attributes of PIPES, PILINGS, TIRES, etc. I want to rename these to Pipes, Pilings, Tires, etc.

I have used relabel() rename.vars() rename() and all run without any error messages, but there is no change in the data. Below is an example of my code.

mat<- memisc::relabel(Data$Material,"PILINGS"=="Pilings","Pipe"=="Pipes","PIPE"=="Pipes","TIRES"=="Tires")

Data$Material_Clean <- NA Data$Material_Clean <- mat

Data$Material_Clean has the exact same attributes as Data$Material with none of the renamed variables.

How do I rename the specified variables?

1

There are 1 best solutions below

0
robbie On

I had the same issue and the best solution I could find was to use setNames from stats:

Data <- setNames(Data, c("Pilings", "Pipes", "Tires"))

Unfortunately, this means you need to include all columns - not just the ones you want to rename. So if you have many columns just get their names as a vector (e.g. using names(Data)), change the names of the ones you want to rename, then pass the updated vector as the second arg to setNames().