-5

I've got a Spatialpointsdataframe. I would like to replace values in a column with NA if in the same row from another column the value is not 1. For example: I've got a coulmn roadtype and a column joincount. If in a row the joincount is not 1, i want to replace the roadtype with NA. Any idea how to do that? Thanks for your help

daverous
  • 101
  • 1
  • So easy, I tried this: shape$name_wegty[which(is.na(shape$Count !=1))] = NA. It did not work though. Any idea why? Thanks a lot – daverous Nov 23 '14 at 12:51
  • Make a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and show the expected result. – talat Nov 23 '14 at 13:05
  • What is it that you think the `is.na` in `is.na(shape$Count !=1)` is doing? – Elin Nov 23 '14 at 13:37

1 Answers1

1

Try

library(sp)
S@coords[,'roadtype'][S@coords[,'jointcount']!=1] <- NA
S
# SpatialPoints:
#     jointcount roadtype
#[1,]          1        3
#[2,]          4       NA
#[3,]          3       NA
#[4,]          1        1
#[5,]          1        4

data

jointcount = c(1,4,3,1,1)
roadtype = c(3,2,5,1,4)
S <- SpatialPoints(data.frame(jointcount,roadtype))
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662