2

Is it possible to somehow easily work with data in Spatial*DataFrame (* = Points, Lines, Polygons, Pixels, Grid,...)? In particular I have difficulties with assigning values to it and to operate with them:

require(gstat)
data(meuse)
coordinates(meuse) = ~x+y
data(meuse.grid)
gridded(meuse.grid) = ~x+y

######## 1) assigning value

meuse[1,'zinc'] <- NA
# Error in meuse[1, "zinc"] <- NA : object of type 'S4' is not subsettable
as.data.frame(meuse)[1,'zinc'] <- NA
# Error in as.data.frame(meuse)[1, "zinc"] <- NA : 
#   could not find function "as.data.frame<-"

######## 2) operating with values

meuse[, 'zinc'] + 2
# Error in meuse[, "zinc"] + 2 : non-numeric argument to binary operator

I have found pretty ugly workarounds for both cases:

# ad 1)
meuse2 <- as.data.frame(meuse)
meuse2[1, 'zinc'] <- NA
meuse2 <- SpatialPointsDataFrame(SpatialPoints(meuse), meuse2)

# ad 2)
as.data.frame(meuse)[, 'zinc'] + 2

but these are just beginners' attempts, way too ugly and complicated... Must be much easier in R!

4
  • 1
    meuse$zinc[1] <- NA It seems that other pathways to this are not provided as methods, but they could be added. I'll have a look into that. You could expect that meuse[1,'zinc'] <- NA, meuse[1,][['zinc']] <- NA, and meuse[1,]$zinc <- NA should all work. It's probably best to avoid the @ operator if you can.
    – mdsumner
    Jul 31, 2013 at 22:02
  • 1
    Also this works so you can avoid $: meuse[['zinc']][1] <- NA
    – mdsumner
    Jul 31, 2013 at 22:07
  • @mdsumner, wow, so many possibilities! But what do you say about the @ operator, why should I avoid it? This is not only interesting but seems important; please post it as your own answer!
    – Tomas
    Jul 31, 2013 at 22:15
  • It's because it is for developers to hide inside the code they provide to users, i.e. it's subverting the API if you don't use the methods provided. There's no danger in it, except that the developers might change the API and your code won't work (I think it's safe in this instance but avoid it generally if you can. The Spatial* classes should have done more to help here, it might be because the DataFrame part was originally implemented separately, before extending the actual data.frame in R, long story I guess).
    – mdsumner
    Aug 1, 2013 at 4:46

2 Answers 2

6

You can do this like this:

meuse$zinc[1] <- NA 

or, better since you can use a variable name rather than literal:

aname <- 'zinc'
meuse[[aname]][1] <- NA

It seems that other pathways to this are not provided as methods, but they could be added. I'm not sure why this is the case, but originally the *DataFrame part was built as an AttributeList in sp since R's actual data.frame was inefficient with rownames at the time, but that is ancient history and all fixed now.

You could expect that these would work, like a normal data.frame but they do not:

meuse[1,'zinc'] <- NA
meuse[1,][['zinc']] <- NA
meuse[1,]$zinc <- NA 

It's probably best to avoid the @ operator if you can, because it is for developers to hide inside the code they provide to users, i.e. it's subverting their API if you don't use the methods provided. There's no massive danger in it, except that the developers might change the API and your code won't work, and you might corrupt the object by modifying one part but not another in the way the object was designed, so you shouldn't use it except privately or in code you maintain actively (I think it's safe in this instance but avoid it generally if you can). See ?"@" and ?"slot" for the basics.

3
  • Thanks! 1) so you think it is a bug that those 3 statements do not work? Can someone fix it? 2) is the @data feature of Spatial*DataFrame documented somewhere? One could take this as a criteria wheter it is reasonable to use it. It seems that they also use the form df[['zinc']][1] intro_sp vignette.
    – Tomas
    Aug 1, 2013 at 6:31
  • What I don't like on these more "correct" ways is that this is not the way I normally use data.frames. The df@data[1, 'zinc'] is close to what I use normally. And more importantly, with @data you can work with it as a dataframe - indexing multiple columns by name or index: meuse@data[,c('zinc','cadmium')] unfortunatelly would not work in the meuse[[]] form.
    – Tomas
    Aug 1, 2013 at 6:38
  • This does work now meuse[1,'zinc'] <- NA introduced in sp 1.0-12
    – mdsumner
    Sep 17, 2013 at 6:48
5

For Spatial*DataFrame objects, you can access the data.frame slot with '@data', and the usual data.frame operations should work. Using your example,

 meuse@data[1, 'zinc'] <- NA

gives

 str(meuse@data)
'data.frame':   155 obs. of  12 variables:
 $ cadmium: num  11.7 8.6 6.5 2.6 2.8 3 3.2 2.8 2.4 1.6 ...
 $ copper : num  85 81 68 81 48 61 31 29 37 24 ...
 $ lead   : num  299 277 199 116 117 137 132 150 133 80 ...
 $ zinc   : num  NA 1141 640 257 269 ...
 ...
0

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.