library(terra)
library(data.table)
# Template raster (3x3 grid)
tmpl <- rast(nrows = 3, ncols = 3, xmin = 0, xmax = 3, ymin = 0, ymax = 3,
crs = "EPSG:4326")
# Sparse table: provide values for a subset of cells
df <- data.table(
cell = c(1L, 3L, 5L, 9L),
v1 = c(10, 20, 30, 40),
v2 = c(100, 200, 300, 400)
)
r <- rastFromDF(df, tmpl)
r
terra::plot(r) # remaining cells are NA
Run the code above in your browser using DataLab