###Raster creation###
rm(list=ls())
library(raster)
## Loading required package: sp
dem <- raster(ncol=3, nrow=3, xmn=100, xmx=115, ymn=100, ymx=115)
ncell(dem)
## [1] 9
res(dem)
## [1] 5 5
valores <- c(50, 45, 50, 30, 30, 30, 8, 10, 10)
(values(dem) <- valores)
## [1] 50 45 50 30 30 30  8 10 10
plot(dem, main = "DEM")
text(dem)

crs(dem) <- CRS('+init=epsg:3115')

Slope and aspect Calculation

(slope = terrain(dem, 'slope', unit='degrees', neighbors=8))
## class      : RasterLayer 
## dimensions : 3, 3, 9  (nrow, ncol, ncell)
## resolution : 5, 5  (x, y)
## extent     : 100, 115, 100, 115  (xmin, xmax, ymin, ymax)
## crs        : +init=epsg:3115 
## source     : memory
## names      : slope 
## values     : 75.25766, 75.25766  (min, max)
plot(slope, main = "pendiente")
text(slope)

(aspecto = terrain(dem, 'aspect', unit= 'degrees', neighbors=8))
## class      : RasterLayer 
## dimensions : 3, 3, 9  (nrow, ncol, ncell)
## resolution : 5, 5  (x, y)
## extent     : 100, 115, 100, 115  (xmin, xmax, ymin, ymax)
## crs        : +init=epsg:3115 
## source     : memory
## names      : aspect 
## values     : 180.7538, 180.7538  (min, max)
plot(aspecto, main = "Aspecto")
text(aspecto)

Assigment

ndem <- raster(ncol=4, nrow=4, xmn=100, xmx=120, ymn=100, ymx=120)
ncell(ndem)
## [1] 16
res(ndem)
## [1] 5 5
valores2 <- c(50, 45, 50, 48, 30, 29, 30, 29, 10, 9, 9, 10, 25, 23, 19, 21)
(values(ndem) <- valores2)
##  [1] 50 45 50 48 30 29 30 29 10  9  9 10 25 23 19 21
plot(ndem, main= "Calcule slope & aspect for this DEM")
text(ndem)