2

I am trying to compute a variogram with the R gstat package:

 > variogram(yield_cleaned~1, data= newdf)

but it turns out:

 Error in .local(obj, ...) : NA values in coordinates

My dataset has NA values. How can I tell the program to ignore them?

  • I am wanting to perform interpolations in R. I am familiar with ArcGIS. @PolyGeo – Gorden Jiang Aug 12 '16 at 3:00
  • Please always include that detail in your questions here. I notice that you have not yet taken our 2-minute Tour to learn about this site's protocols. – PolyGeo Aug 12 '16 at 3:01
  • Sorry my mistake...@PolyGeo – Gorden Jiang Aug 12 '16 at 3:28
2

You should remove your NA in your data frame first using the function na.omit (see this: remove-rows-with-nas-in-data-frame) and then apply the variogram function.

# Load libraries
library('gstat')

# Example data
newdf <- data.frame("id" = 1:10,
                    "yield_cleaned" = runif(n = 10, min = 0, max = 1),
                    "x" =  sample(x = c(1:8, rep(NA,2)), size = 10),
                    "y" = sample(x = c(1:8, rep(NA,2)), size = 10)
                    )

# Remove NA rows
newdfNoNAs <- na.omit(object = newdf)

# Apply variogram function 
varData <- variogram(yield_cleaned~1, data = newdfNoNAs, locations = ~x+y)

I also suppose that make a variogram with no spatial information (NA values in your coordinates data) have no sense.

  • 3
    Please note that if the data is an sp class object, then this would be a issue because you would be removing row in the @data slot without removing the corresponding spatial element. This results in a mismatch between the slots in the object. – Jeffrey Evans Aug 12 '16 at 18:01
  • newdf returns 10 points and newdfNoNAs returns 6 points, Could you briefly explain why varData returns two points? @Guzmán – Gorden Jiang Aug 14 '16 at 22:35
  • My data is in a data frame and na.omit has removed the corresponding coordinates as well. My varData <- variogram(yield_cleaned~1, data = newdfNoNAs, locations = ~lon+lat) seems to take ages to run. Any solution for this? My dataset has 203140 obs after na.omit(). @Guzmán @Jeffrey Evans – Gorden Jiang Aug 14 '16 at 22:52
  • @GoldenJiang varData returns two points because of the default cutoff value (the length of the diagonal of the box spanning the data is divided by three). Try for example: varData <- variogram(yield_clea‌​ned~1, data = newdfNoNAs, locations = ~lon+lat, cutoff = 6) – Guzmán Aug 15 '16 at 16:28
  • @GoldenJiang I had the same problem with a dataset of 287822 observations. You can just wait or clip your dataset by some extent and do it by parts. – Guzmán Aug 15 '16 at 16:34

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.