Good afternoon,
I have been using R's terra package, and have been looking for a way to access an individual cell's value.
I would have expected raster_1[1, 2, 1]
for example, to return a numeric value telling me which value does the cell located on the 1st row of the 2nd column of the 1st layer have, but instead I get
mean
1 2.106639
Is this behaviour expected? What is this mean value anyway, shouldn't [row, col, layer] be returning one numeric value instead of a matrix?
Thank you very much for your time!
As mentioned by @margusl, your result is showing your the expected value, but it has named that value with the name of the layer. Per the Terra help doc, the syntax is x[i, j, k]
where i
is the row, j
is the column, and k
is the layer. We can make sure this is returning the expected results with a reproducible example.
library(terra)
#> terra 1.7.39
r <- c(
rast(matrix(1:100, nrow=10, ncol=10)),
rast(matrix(201:300, nrow=10, ncol=10)),
rast(matrix(301:400, nrow=10, ncol=10))
) |>
setNames(c("layer1", "layer2", "layer3"))
r[1, 2, 1]
#> layer1
#> 1 11
r <- setNames(r, c("mean", "a", "b"))
r[1, 2, 1]
#> mean
#> 1 11
Just as in your example "mean" isn't referring to a function name, but to a layer name.