I'm trying, in the package terra, to subset a raster by row and column numbers. Apparently that is easy in raster, at least without a geographic extent and crs: Subset a raster using row/column index. But I can't get it to work in terra. There has to be an easy way. terra::subset only selects layers of a raster.
Expecting someone to ask why: I padded a raster with rows and columns before sampling an elevation raster and calculating slope and aspect, which relies on neighboring cells. Now I need to strip off those padded rows and columns.
library(terra)
EXT <- c( -108, -105, 39, 42 )
R <- rast( extent=EXT, ncol=14, nrow=14, crs="epsg:4326" )
R[] <- 1:ncell(R)
# Now try to strip off the outer 2 rows and columns
crop( x=R, y=ext( 3, 12, 3, 12 ) )
# Error: [crop] extents do not overlap
# Normal R-style subsetting also does not work,
# just gives values of that subset
R[ 3:12, 3:12 ]