2

I'm getting an error when trying to add a new field (of deciles of my income variable) to my shapefile within R. The error might be related to the fact that the input is a shapefile...

UseMethod("mutate_") : no applicable method for 'mutate_' applied to an object of class "c('SpatialPolygonsDataFrame', 'SpatialPolygons', 'Spatial')

require(rgdal)
#input data SES field
mydata<-readOGR("C:\\Users\\Desktop\\mydata.shp")
library(dplyr)
deciles = mutate(mydata, quantile_rank = ntile(mydata$income,10))
deciles
2

What "they" didn't teach you about R is that instead of "mutate":

deciles = mutate(mydata, quantile_rank = ntile(mydata$income,10))
deciles

which only works sometimes, you can always create the new column directly:

deciles$quantile_rank = ntile(mydata$income,10)
deciles

Your input is an sp class "SpatialPolygons Data Frame", which has been read in from a shapefile. This behaves a bit like a data frame in most respects, but dplyrs functions do not support it.

|improve this answer|||||
  • It works! mydata$quantile_rank = ntile(mydata$income,10) – bkGeog Feb 17 at 0:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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