0

I'm trying to import a text file into R, specifically the spatstat package. I've loaded a shp file as the window and that worked just fine (displays with plot() ). I can't get the ppp() command to run though. I keep getting this error after running pp <- ppp(X, Y, window=W)

Error in ppp(X, Y, window = W) : 
  1 out of 904 coordinate values are NA or NaN

I've double-checked the file and neither of the X or Y coords have any blank numbers or even negative numbers.

What should I check to deal with this error? Also, this data is public so I can give it to anybody if they need to have a look at it.

Josh
  • 3,385
  • 5
  • 23
  • 45

2 Answers2

1

It would be better to check the dataset loaded from the file rather than the file itself. There can be a lot of tricky things in the txt that are hard to catch by eye, a space delimiter instead of a tab, an extra '\n' at the end, etc.

Try a

which(is.nan(X))

It looks like there's just one observation giving you a problem.

joran
  • 169,992
  • 32
  • 429
  • 468
kith
  • 5,486
  • 1
  • 21
  • 21
  • 1
    I think you mean `is.nan`. And based on the error message, they should probably check `is.na` as well. – joran Mar 01 '13 at 17:32
  • This will not work if X as infinite value. `any(is.infinite(X)) > 0` – agstudy Mar 01 '13 at 18:01
  • `is.na` worked. There was an error on the 904th line which is why it gave it as 1 out of 904 when there was over 2000 points. That should have tipped me off frankly. Interestingly enough, it was an apostrophe in a text field that was the problem. Thanks a bunch. – Josh Mar 02 '13 at 14:07
0

I would something like this :

ok <- is.finite(X) & is.finite(Y)
if(!ok){
    X <- X[is.finite(X)]
    Y <- Y[is.finite(Y)]
}
pp <- ppp(X, Y, window=W)
agstudy
  • 119,832
  • 17
  • 199
  • 261