sample() function to create the numbers.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
fancy_matrix <-
sample(1:1000, 8*8, replace = TRUE) %>%
matrix(nrow = 8, ncol = 8)
fancy_matrix
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 27 644 255 829 710 865 123 7
## [2,] 927 189 340 780 425 607 450 518
## [3,] 92 967 67 431 816 715 983 501
## [4,] 68 235 829 926 549 748 253 383
## [5,] 631 519 8 489 465 162 258 1000
## [6,] 201 334 741 295 940 364 217 51
## [7,] 406 909 872 425 319 323 569 173
## [8,] 614 874 243 325 826 237 284 35
raster() function can be fed with matrices to create a raster layer.
library(raster)
## Loading required package: sp
##
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
##
## select
fancy_raster_layer <-
raster::raster(fancy_matrix)
raster::plot(fancy_raster_layer)
The raster() function can not only be used to create raster data on the fly, which is also not very interesting. Instead, we can use it to import already prepared data.
.tiff files in the ./data/ folder of the workshop directory (repository). Do you get any warning messages?
getwd(). Setting is done with setwd().
immigrants_cologne <-
raster::raster("../data/immigrants_cologne.tif")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
# The warning message is due to the current transition of CRS definition
# systems. You can ignore it in this course.
+ and - operators.
# load all layers
immigrants_cologne <-
raster::raster("../data/immigrants_cologne.tif")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
germans_cologne <-
raster::raster("../data/germans_cologne.tif")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
inhabitants_cologne <-
raster::raster("../data/inhabitants_cologne.tif")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
# create sum layer
immigrants_germans_sum <-
immigrants_cologne + germans_cologne
# create difference layer
difference_layer <-
inhabitants_cologne - immigrants_germans_sum
difference_layer
## class : RasterLayer
## dimensions : 289, 264, 76296 (nrow, ncol, ncell)
## resolution : 100, 100 (x, y)
## extent : 4094850, 4121250, 3084050, 3112950 (xmin, xmax, ymin, ymax)
## crs : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs
## source : memory
## names : layer
## values : -2, 0 (min, max)
# get a summary statistic
summary(difference_layer)
## layer
## Min. -2
## 1st Qu. 0
## Median 0
## 3rd Qu. 0
## Max. 0
## NA's 62522
# create a table of counts
difference_layer %>%
as.data.frame() %>%
table()
## .
## -2 -1 0
## 11 2052 11711