1

I have a raster like this here: https://drive.google.com/drive/u/0/folders/1AEKu4TKpzJg95oXv8dxep24xpXaesr0F

The tif has 11 Layers (timestamps). For each pixel I want to find the timestamp with the maximum value. For some reason one approach works with that data, one does not.

r = rast("au.tif")
terra::app(r, which.max) # works
terra::app(r, function(x){which.max(x)}) # does not work

For the second approach I get this error which I am not really sure about:

Error: Not compatible with requested type: [type=list; target=double].

If I am using another raster, both work. So I was wondering why that might be:

b = rast(system.file("ex/logo.tif", package="terra"))
terra::app(b, which.max) # works
terra::app(b, function(x){which.max(x)}) # works also

Becua

1 Answer 1

2

Its because there are pixels that are all NA.

b = rast(system.file("ex/logo.tif", package="terra"))
terra::app(b, function(x){which.max(x)} 

works but...

b[[1]][1,1] = NA
b[[2]][1,1] = NA
b[[3]][1,1] = NA
terra::app(b, function(x){which.max(x)}) 

fails:

Error: Not compatible with requested type: [type=list; target=double].

I think this is because R will be calling which.max on a vector of NAs and returning a length-0 vector, which messes up how app puts things together.

Also, you can just do which.max(b) since there's a method for terra rasters.

1
  • Ah yes I thanks a lot! I tested the which.max function on a vector that contains NAs and it didnt fail. But makes sense of course:)
    – Lenn
    Commented Dec 8, 2023 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.