5

This question already has an answer here:

To simplify my problem, I'm gonna start at the source instead of explaining my full-map problem.

I have a shapefile of Ethiopia pulled from GADM (can be downloaded here). A lot of the attribute columns are unnecessary and just create extra clunkiness as I perform clips/joins/etc. with the shapefile. So I want to remove all but the first 4 columns. Doing this seems easy enough, and I tried and succeeded with this bit of code:

library(rgdal)

#read in shapefile
eth <- readOGR(dsn = "D:/Mapping-R/Ethiopia", layer = "ETH_adm0")

#subset data/remove columns
eth <- eth@data[, -(5:67)]

This does indeed leave me only with the first 4 columns, but it also makes the object a regular data frame rather than a spatial data frame ready for mapping. So how do I remove these unwanted columns while maintaining status of SPDF?

marked as duplicate by Andre Silva, LaughU, BERA, xunilk, csk Jan 2 at 18:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    eth@data <- eth@data[, -(5:67)] – hrbrmstr Dec 11 '15 at 21:25
  • 1
    Eck!! Don't use slots! eth <- eth[, -(5:67)] – mdsumner Dec 11 '15 at 21:45
  • 1
    Also, see ?raster::getData to get this data dynamically. – mdsumner Dec 11 '15 at 21:46
  • To expand on @hrbrmstr correct answer, you are replacing the sp object with the associated data.frame that you are subsetting. You want the <at>data slot to operate on the <at>data slot which contains a data.frame object. So, you need the call the slot on both sides of the operator. In previous version of sp not accessing the <at>data slot would throw an error but now you can also follow the other recommendation as it is correct in current versions. However, there are cases, specifically with indexing, where you need to directly call the <at>data slot. – Jeffrey Evans Dec 11 '15 at 22:05
  • 1
    It is the same in this case (although I would do eth <- eth[, 1:4]). But using the @ is not safe because you directly make changes in the internal structure of an object without considering the consequences. Perhaps other values also need to change somewhere in that structure? I do not think that is the case here, but it is best to leave that to the developers to think about and for you to use the functions they provide to do the task at hand. – Robert Hijmans Dec 12 '15 at 18:30
4

Untested.

library(rgdal)

#read in shapefile
eth <- readOGR(dsn = "D:/Mapping-R/Ethiopia", layer = "ETH_adm0")

#subset data/remove columns
eth <- eth[, -(5:67)] 

Don't use "@" slots, they are for developers. Here's why:

library(sp)
data(meuse)
coordinates(meuse) <- c("x", "y")
# legal, but totally borked
meuse@data <- meuse@data[1:2, ]
meuse  ## whoops
Error in data.frame(coordinates = cc, x@data) : 
 arguments imply differing number of rows: 155, 2
  • 1
    I do not agree with not using the slot call. In most cases it is the safest approach. – Jeffrey Evans Dec 11 '15 at 21:58
  • 1
    That's crazy talk. – mdsumner Dec 11 '15 at 22:13
  • We can certainly agree to disagree but I am an R package developer (5 and counting) and there are cases where you absolutely have to call the data slot. Your example does not make much sense because you are subsetting the first two rows and not columns, which I would just consider a syntax error and not an argument for not using slots. Besides, using your logic, how would you append a column? – Jeffrey Evans Dec 11 '15 at 22:29
  • 1
    meuse$newcol <- seq(nrow(meuse)) There might be missing functionality in sp's methods (think of print for example) but it's time to send a request to the authors, not subvert the design and propagate bad code.A developer will use the slots in methods obviously, not on the interwebs. Every time you do it you should caveat it with warnings IMO. – mdsumner Dec 11 '15 at 22:43
  • 2
    R does not allow to make slots private, which invites users or developers to either do great things, or do terrible things, from the perspective of the sp developers. Mike's example above is syntactically correct, but creates a meaningless object. validObject(meuse) generates an error after it, but it is not called automatically when a slot is assigned. As with R's data.frame, some of the design is implicit in the implementation, we'll have to live with that. I welcome requests to authors. – Edzer Pebesma Dec 14 '15 at 12:50
-2

Right click to go to properties to see the field data and unchecked the unnecessary columns,make sure you do not unchecked the Shape and length.

Export the shapefile to another location and open it again to check if you still have the those columns. You should be able to perform your task.

  • 2
    This is not a solution addressing the OP's question on how to do this in R and you do not even note what software you are referring to. – Jeffrey Evans Dec 14 '15 at 18:09

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