Introduccion
Configuracion de libreria
# NUEVO CUADERNO
library(terra)
## Warning: package 'terra' was built under R version 4.2.3
## terra 1.7.71
Creación de raster desde Cero
# Creacion de raster desde cero
r <- rast(nrows=40, ncols=20, xmin=0, xmax=10, ymin=0, ymax=10)
r
## class : SpatRaster
## dimensions : 40, 20, 1 (nrow, ncol, nlyr)
## resolution : 0.5, 0.25 (x, y)
## extent : 0, 10, 0, 10 (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84
values(r) <- runif(ncell(r))
v <- values(r)
dim(v)
## [1] 800 1
head(v)
## lyr.1
## [1,] 0.8764688
## [2,] 0.8028081
## [3,] 0.5854401
## [4,] 0.9149462
## [5,] 0.5530419
## [6,] 0.3709626
plot(r)

MANIPULACION SpatRaster
#MANIPULAR SpatRaster
q <- 100 * r^2
plot(q)

# create a binary mask based on a logical expression
m <- q < 10
plot(m)

p <- c(r, q)
# assign meaningful names to the layers
names(p) <- c ("random", "scaled_random")
plot(p)

ma <- mean(p)
plot(ma)

global(p, fun="mean")
## mean
## random 0.5045257
## scaled_random 33.8800103
r[r < 0.5] <-0.5
plot(r, range=c(0,1))

myFunction <-function(x){x[x <=0.5] <- 10; return(x)}
w <- app(r, fun=myFunction)
plot(w, range=c(0,1))
