I am trying to select several rasters from a SpatRasterDataset using the terra package in R. However when I try to select x rasters at once, the names show only the first x raster names, regardless of which rasters I try to select. Selecting a single raster seems to work fine, although the name given to it is not present.
So how do I properly subset a SpatRasterDataset keeping the correct names associated with rasters?
Reproducible example:
# Create SpatRasterDataset
r1 <- rast(matrix(1:100, nrow=10, ncol=10))
r2 <- rast(matrix(200:101, nrow=10, ncol=10))
r3 <- rast(matrix(c(2,200), nrow=10, ncol=10))
s <- sds(r1,r2,r3)
# Name rasters
names(s) <- c("A","B","C")
names(s)
# Add to list
l <- list(s)
l[[1]]
l[[1]][1:2] # shows rasters A,B
l[[1]][2:3] # also shows rasters A,B
l[[1]][c("B","C")] # shows rasters A,B
l[[1]][3]
image(l[[1]][3]) # shows correct raster
# Subset SpatRasterDataset
s2 <- l[[1]][2:3] # select two rasters
names(s2) # names A, B
s2[1] # actually raster B
s2[2] # actually raster C
s3 <- l[[1]][3] # select one rasters
names(s3) # has some other name
s3 # has correct raster