I am following the very simple code found in this question:

Converting shapefile to raster: Character attributes

to create a new numeric variable based on a factor. My problem is that the new numeric attribute has a complete different series of numbers than the factor column.

For example, instead of reading: 18001, 18002, 18003, it converts these numbers into 786, 894, 895: I have tried as.numeric and as.integer, and none of these work.

For further details, the shapefile column is described as:

commune_5m@data$NUMERO: Factor w/ 1842 levels "18001", "18002", ...: 786 894 895

1 Answer 1


Sorry, The solution was very simple, I needed to convert to character before converting to numeric:

commune_5m@data$NUMERO2 = as.numeric(as.character(commune_5m@data$NUMERO)) 
### Create numeric attribute from factor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.