r <- rast(ncols=5, nrows=5, xmin=0, xmax=5, ymin=0, ymax=5)
values(r) <- 1:25
xy <- rbind(c(0.5,0.5), c(2.5,2.5))
p <- vect(xy, crs="+proj=longlat +datum=WGS84")
extract(r, xy)
extract(r, p)
r[1,]
r[5]
r[,5]
r[c(0:2, 99:101)]
f <- system.file("ex/meuse.tif", package="terra")
r <- rast(f)
xy <- cbind(179000, 330000)
xy <- rbind(xy-100, xy, xy+1000)
extract(r, xy)
p <- vect(xy)
g <- geom(p)
g
extract(r, p)
x <- r + 10
extract(x, p)
i <- cellFromXY(r, xy)
x[i]
r[i]
y <- c(x,x*2,x*3)
y[i]
## extract with a polygon
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
v <- v[1:2,]
rf <- system.file("ex/elev.tif", package="terra")
x <- rast(rf)
extract(x, v, mean, na.rm=TRUE)
z <- rast(v, resolution=.1, names="test")
values(z) <- 1:ncell(z)
e <- extract(z, v, ID=TRUE)
e
tapply(e[,2], e[,1], mean, na.rm=TRUE)
x <- c(z, z*2, z/3)
names(x) <- letters[1:3]
e <- extract(x, v, ID=TRUE)
de <- data.frame(e)
aggregate(de[,2:4], de[,1,drop=FALSE], mean)
Run the code above in your browser using DataLab