I have a spatraster
library(terra)
r
class : SpatRaster
dimensions : 6000, 6000, 1 (nrow, ncol, nlyr)
resolution : 0.0008333333, 0.0008333333 (x, y)
extent : 89.99958, 94.99958, 4.999583, 9.999583 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
source : memory
name : SSP_2050
min value : 0.0100
max value : 0.2376
When I try to convert it into dataframe, I get below error:
r_df <- terra::as.data.frame(r, xy = T, na.rm = F)
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 36000000, 43596000
Why is this happening?
That looks like a bug and this is not the place to report that. (See CRAN for where to go)
Either way, I cannot reproduce this. Can you create a reproducible example that creates this error? Are you perhaps using an old version of "terra"? And what does this return? length(values(r))
. I am guessing that it is 43596000
where it should be 36000000
. How was your r
created?
library(terra)
terra 1.6.17
r <- rast(ncol=5, nrow=2)
values(r) <- 1:10
r[1:2] <- NA
as.data.frame(r, xy=T, na.rm=T) |> head()
# x y lyr.1
#3 0 45 3
#4 72 45 4
#5 144 45 5
#6 -144 -45 6
#7 -72 -45 7
#8 0 -45 8
Here is a work-around
p <- as.points(r)
x <- cbind(crds(p), values(p))
head(x)
# x y lyr.1
#1 0 45 3
#2 72 45 4
#3 144 45 5
#4 -144 -45 6
#5 -72 -45 7
#6 0 -45 8