- All objects in R have a class
- Classes can be combined
- Spatial class systems
Slides: github.com/ITSLeeds/R4TA
x = 1 class(x)
## [1] "numeric"
typeof(x)
## [1] "double"
dim(x)
## NULL

Ctl-Enter\[ \pi * 9.15^2 \]
Alt-Shift-K on Windows/Linux)In R:
x = 5
sin(x)
## [1] -0.9589243
exp(x)
## [1] 148.4132
sinx = sin(x)
plus1 = function(x) {
x + 1
}
plus1(x)
## [1] 6
x = c(1, 2, 5) x
## [1] 1 2 5
x^2
## [1] 1 4 25
x + 2
## [1] 3 4 7
x + rev(x)
x = c(1, 2, 5)
for(i in x){
print(i^2)
}
## [1] 1 ## [1] 4 ## [1] 25
Creating a new vector based on x
for(i in 1:length(x)){
if(i == 1) x2 = x[i]^2
else x2 = c(x2, x[i]^2)
}
x2
## [1] 1 4 25
R has a hierarchy of data classes, tending to the lowest:
a = TRUE b = 1:5 c = pi d = "Hello Leeds"
class(a) class(b) class(c) class(d)
ab = c(a, b) ab
## [1] 1 1 2 3 4 5
class(ab)
## [1] "integer"
class(c(a, b))
## [1] "integer"
class(c(a, c))
## [1] "numeric"
class(c(b, d))
## [1] "character"
x = 1:5 y = 2:6 plot(x, y)
x = seq(1,2, by = 0.2) length(x)
## [1] 6
x = seq(1, 2, length.out = 5) length(x)
## [1] 5
x = 1:10 x[c(1, NA)] x[2 * 3] x[-2 * 3]
sf is a package and class system for geographic data
library(sf)
## Linking to GEOS 3.5.1, GDAL 2.2.2, proj.4 4.9.2
Enables geographic data creation, e.g. with:
x = 1:9
y = x^2
xdf = data.frame(v1 = x, x = x, y = y)
xsf = st_as_sf(x = xdf, coords = c("x", "y"))

class(xsf)
## [1] "sf" "data.frame"
names(xsf)
## [1] "v1" "geometry"
xsf$geometry[1]
## Geometry set for 1 feature ## geometry type: POINT ## dimension: XY ## bbox: xmin: 1 ymin: 1 xmax: 1 ymax: 1 ## epsg (SRID): NA ## proj4string: NA
## POINT (1 1)
unclass(xsf$geometry[1][[1]])
## [1] 1 1
attributes(xsf$geometry[1][[1]])
## $class ## [1] "XY" "POINT" "sfg"
(xy = st_point(c(5, 2))) # XY point
## POINT (5 2)
(xyg = st_sfc(xy))
## Geometry set for 1 feature ## geometry type: POINT ## dimension: XY ## bbox: xmin: 5 ymin: 2 xmax: 5 ymax: 2 ## epsg (SRID): NA ## proj4string: NA
## POINT (5 2)
(xysf = st_sf(xyg))
## Simple feature collection with 1 feature and 0 fields ## geometry type: POINT ## dimension: XY ## bbox: xmin: 5 ymin: 2 xmax: 5 ymax: 2 ## epsg (SRID): NA ## proj4string: NA ## xyg ## 1 POINT (5 2)
Talk about the pros and cons of vector data in your field of application with your neighbour: when would you be persuaded to use raster data?
Work through section 2.1 of Chapter 2, focussing on the code (not the text)
Complete the exercises 1:3 in Chapter 2
Bonus: work through section 20.4 in r4ds and complete the exercises in section 20.4.6