Skip to main content
2 of 2
added 375 characters in body

Getting an error when trying to set extent for raster() in R

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?