Welcome to GEOG70922. On this course, lab instructions will be posted as html and a web link will be provided each week for you to work through in the practical sessions.
Open R studio from the Start Menu and pres Ctrl + Shift + N. A new script will open. Have a look around the R Studio IDE.
Code for you to enter in R Studio will be presented against a grey background. When you see this, it means it’s time for you to do something (i.e. type or copy and paste the code into your script in the program pane).
Now type the following code and with the cursor at the end of your line, press Ctrl+Enter:
print("Hello world")
## [1] "Hello world"
Now try a basic calculation:
2*2
## [1] 4
R is an object oriented language designed for statistical computing and graphics. The main “building blocks” for working with data in R are the objects which store our information of interest. Objects come in various “classes” such as vectors, data frames and lists as well as more fundamental GIS objects such as raster layers and spatial points layers.
Vectors in R consist of a series of elements of the same type and are generally formulated with the c() function, a useful function for combining elements together. Creating an object is done using “object<-” such that creating a vector of integers called x is done as follows:
x<-c(9,8,7,6,5,4,3,2,1)
x
## [1] 9 8 7 6 5 4 3 2 1
Note that the : operator can be useful when creating vectors:
y<-c(1:9)
y
## [1] 1 2 3 4 5 6 7 8 9
Let’s combine x and y into a data frame and plot them:
df<-data.frame(cbind(x,y))
plot(df)
Now let’s look at some spatial data. Call the ‘sf’ library to create some basic geometries in R:
install.packages("sf")
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
pol_points=rbind(c(10,10),c(10,9),c(11,9),c(11,10),c(10,10))
pol=st_polygon(list(pol_points))
plot(pol)
pol_points2=rbind(c(12,12),c(13,13),c(14,12),c(12,12))
mpol=st_multipolygon(list(list(pol_points),list(pol_points2)))
plot(mpol)
Now continue with something more meaningful. We will use the st_linestring() and st_multlinestring() functions to join our points that we collate together using the list() function. Type each chunk of code into your script and run separately, see if you can guess what comes next.
ps = rbind(c(10,9),c(10,10))
l = st_linestring(ps)
plot(l)
ps2=rbind(c(11,10),c(11,9),c(12,9))
mls<-st_multilinestring(list(ps,ps2))
plot(mls)
ps3=rbind(c(13,10),c(14,10),c(14,9),c(13,9),c(13,10))
pol=st_multilinestring(list(ps,ps2,ps3))
plot(pol)
ps4=rbind(c(15,10),c(15.5,9),c(16,10))
mls<-st_multilinestring(list(ps,ps2,ps3,ps4))
plot(mls)
ps5=rbind(c(18,10),c(17,10),c(17,9),c(18,9))
mls<-st_multilinestring(list(ps,ps2,ps3,ps4,ps5))
ps6=rbind(c(17,9.5),c(18,9.5))
mls<-st_multilinestring(list(ps,ps2,ps3,ps4,ps5,ps6))
plot(mls)
ps7=rbind(c(20,10),c(19,10),c(19,9),c(20,9),c(20,9.5),c(19.5,9.5))
mls<-st_multilinestring(list(ps,ps2,ps3,ps4,ps5,ps6,ps7))
plot(mls)
ps8 = rbind(c(21,9),c(21,10))
mls=st_multilinestring(list(ps,ps2,ps3,ps4,ps5,ps6,ps7,ps8))
plot(mls)
ps9=rbind(c(23,10),c(22,10),c(22,9.5),c(23,9.5),c(23,9),c(22,9))
mls<-st_multilinestring(list(ps,ps2,ps3,ps4,ps5,ps6,ps7,ps8,ps9))
plot(mls)
Now you’re well on your way to geo-spatial analysis in R! Check the blackboard page for more updates and tips. You might also like to explore some of the helpful online resources that can be found here: [https://education.rstudio.com/learn/beginner/]. See you in Week One.