Getting started with R

Using R in RStudio

Basic concepts in R

Maps in R

## You can follow along with the example code in this lecture if you ### Open [this R script](demo/starting-r.R?raw=true) in _RStudio_ ### Also download this [data file](demo/welly.geojson?raw=true) and save it in the same folder
# Using *R* in *RStudio* ## [*R*](https://www.r-project.org/) is a *platform for statistical computing* ## [*RStudio*](https://posit.co/products/open-source/rstudio/) is an environment we run *R* in to make using it easier ## Both are available free (as in beer), but *RStudio* is commercial software ## *R* and its many, many contributed packages are free (as in speech)
# As an aside ## *R* might be Aotearoa New Zealand’s [most important contribution to science](https://www.stat.auckland.ac.nz/~ihaka/downloads/R-paper.pdf) since Rutherford split the atom

Console

Outputs

Environment/
history

Scripts/
other files

image source boingboing.net

Basic concepts in R

Variables

Values, vectors and dataframes

Plotting

Packages

Variables

A variable is a name that we associate data with, by assigning data to a name, using the assignment operator <-


            x <- 4
            y <- 5.2
            z <- "This is text"
            

Values, vectors and dataframes

If we could only assign individual values, R would not be very useful


            x <- 3
            y <- c(1, 45, 27.3, -123) # y is a vector of values
            z <- data.frame(a = 1:100,
                            b = rep(5,100),
                            c = runif(100)) # z is a dataframe
            

Dataframes

Dataframes are how we manipulate and manage tables of data in R

Each row is a data point, and each column is an attribute

We can access attributes using the dataframe name and the attribute name joined by a $ symbol


            names(z)
            z$a
            

One of the most powerful things about R is that we can apply functions to all the members of a vector or dataframe


            x ^ 2 # ^ raises to the power
            y ^ 2
            z ^ 2
            z$c ^ 2
            

Plotting data

R has powerful built in plotting functions for visualizing data


            plot(x, x^2)
            plot(z)
            boxplot(z$c)
            hist(z$c)
            

# *R* is extensible through *packages* ## These are loaded using `library(packagename)` ## There are many packages for handling spatial data ## We will be looking closely at `sf` and `tmap` among others
# Maps in *R* ## `sf` for handling spatial datasets ## `tmap` for making maps from them

A taster


            library(sf)
            sf <- st_read("welly.gpkg")
            names(welly)
            plot(welly)

            library(tmap)
            tm_shape(welly) + tm_polygons()
            tm_shape(welly) + tm_polygons(col = "PopUR13")
            

Summary

Key concepts are variables, vectors, dataframes, and extensibility through packages

sf and tmap packages will be focus of next couple of weeks on making maps in code

More on this in lab