Simply use a SpatialPixelsDataFrame and the rasterFromXYZ function of the raster package to create the raster ( Creating a DEM from regularly / irregularly spaced points (R and Python) )
1) With your solution
data = read.table("test.txt", h = T, sep = ",") # example with regularly spaced points
library(raster)
X = data$x
Y = data$y
Z = data$z
data = matrix(c(X,Y,Z), ncol=3, byrow=FALSE)
e = extent(data[,1:2])
r=raster(e, ncol=3, nrow=25, crs = CRS("+init=epsg:31370"))
x = rasterize(data[,1:2], r, data[,3], fun=mean)
plot(x)
And
slope_asp = terrain(x, opt=c('slope', 'aspect'), unit='degrees', neighbors=8)
summary(slope_asp)
[,1] [,2]
Min. NA NA
1st Qu. NA NA
Median NA NA
3rd Qu. NA NA
Max. NA NA
NA's 75 75
2) with a SpatialPixelsDataFrame and the rasterFromXYZ function
df = data.frame(X,Y,Z)
theraster = rasterFromXYZ(df)
crs(theraster) = "+init=epsg:31370"
plot(theraster)
Now you can compute a valid slope
slope_asp = terrain(theraster, opt=c('slope', 'aspect'), unit='degrees', neighbors=8)
summary(slope_asp)
[,1] [,2]
Min. 25 225
.....
Best Answer
If you search for Terrain analysis in the Processing toolbox you will find what you want. The tool moved from
Raster -> Terrain analysis
toProcessing Toolbox -> Raster Terrain analysis
:I am using QGIS 3.4