3

I'm working through Robert's answer to this question on how to plot irregular raster data: How to make RASTER from irregular point data without interpolation

no2tc <- ncvar_get(nc, "DETAILED_RESULTS/nitrogendioxide_total_column")
lat <- ncvar_get(nc, "PRODUCT/latitude")
lon <- ncvar_get(nc, "PRODUCT/longitude")

no2vec <- as.vector(no2tc)
latvec <- as.vector(lat)
lonvec <- as.vector(lon)

df <- data.frame(lonvec,latvec)
colnames(df) <- c('X', 'Y')
e <- extent(df)

My data very similar in format to the fake data created in his question and I got the following error:

Error in extent(df[, 2:3]) : c("x", "y") %in% names(x) are not all TRUE

Here is a sample of my data

     Z         X          Y
1   NA -84.17967 -118.36866
2   NA -84.22738 -117.62881
3   NA -84.27346 -116.88749
4   NA -84.31795 -116.14489
5   NA -84.36089 -115.40117
6   NA -84.40232 -114.65651
7   NA -84.44226 -113.91109
8   NA -84.48075 -113.16512
9   NA -84.51782 -112.41875


> df[,2:3]
            X             Y
1   -84.17967 -118.36865997
2   -84.22738 -117.62880707
3   -84.27346 -116.88748932
4   -84.31795 -116.14488983
5   -84.36089 -115.40116882

The data has some NA values but obviously, I would want to ignore these.

Can someone tell me why I might be getting this error?

2
  • You need to show us your code that is causing the error - the linked question might get deleted in the future making this question unanswerable! – Spacedman Mar 13 at 15:48
  • @Spacedman Noted and added. – Priya Patel Mar 13 at 16:13
3

At the point you compute the extent, your data frame has names "X" and "Y", like this one:

> df = data.frame(X=runif(10), Y=runif(10))

So you get this error:

> e = extent(df)
Error in extent(df) : c("x", "y") %in% names(x) are not all TRUE

If instead the names are lower case x and y, then extent works:

> names(df)=c("x","y")
> e = extent(df)
> e
class      : Extent 
xmin       : 0.1511693 
xmax       : 0.9194653 
ymin       : 0.01122255 
ymax       : 0.9353158 
1
  • So.....simple. Thank you! – Priya Patel Mar 13 at 17:00

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.