WEAK # 2: Advance Topics in GIS

Basics in R

Zulfiqar Ali, Ph.D (Assistant Professor of Statistics, CSS, University of the Punjab, Lahore)

Basics (Session 1)

In this Session, you will take your first steps with R. You will learn how to use the console as a calculator and how to assign variables. You will also get to know the basic data types in R!

Note

You can execute R commands straight in the console. For example

Calculate 8 + 9

8+9
## [1] 17

Calculate 6 + 12

6 + 12
## [1] 18

Arithmetic with R

In its most basic form, R can be used as a simple calculator. Consider the following arithmetic operators:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Exponentiation: ^
  • Modulo: %%

* The ^ operator raises the number to its left to the power of the number to its right: for example 3^2 is 9.

* The modulo returns the remainder of the division of the number to the left by the number on its right, for example 5 modulo 3 or 5 %% 3 is 2.

With this knowledge, follow the instructions below to complete the exercise.

Instructions

  • Type 2^5 in the editor to calculate 2 to the power 5.
  • Type 28 %% 6 to calculate 28 modulo 6.
  • Click ‘Submit Answer’ and have a look at the R output in the console.
  • Note how the # symbol is used to add comments on the R code.

An addition

5 + 5 
## [1] 10

A subtraction

5 - 5 
## [1] 0

A multiplication

3 * 5
## [1] 15

A division

(5 + 5) / 2 
## [1] 5

Exponentiation

2 ^ 5
## [1] 32

Modulo

28 %% 6
## [1] 4

Variable assignment

A basic concept in (statistical) programming is called a variable.

A variable allows you to store a value (e.g. 4) or an object (e.g. a function description) in R. You can then later use this variable’s name to easily access the value or the object that is stored within this variable.

You can assign a value 4 to a variable my_var with the command: my_var <- 4

Instructions

Over to you: complete the code in the editor such that it assigns the value 42 to the variable x in the editor. Click ‘Submit Answer’. Notice that when you ask R to print x, the value 42 appears.

Assign the value 42 to x

x <- 42

Data Input in R

    1. Using Scan() command
    1. Using Import functions
    1. Using links

Data or results exporting from R

    1. Copy past
    1. Using write functions

Exporting Graphs and Setting

(Perform experiments)

Session 2

Vectors

In this Session, you will learn how to work with vectors in R! After completing this lecture, you will be able to create vectors in R,name them, select elements from them and compare different vectors.

Create a vector

Vectors are one-dimension arrays that can hold numeric data, character data, or logical data. In other words, a vector is a simple tool to store data. For example, you can store your daily gains and losses in the stock market.

In R, you create a vector with the combine function c(). You place the vector elements separated by a comma between the parentheses. For example:

numeric_vector <- c(1, 2, 3)
character_vector <- c("a", "b", "c")

Once you have created these vectors in R, you can use them to do calculations.

Instructions

Complete the code such that boolean_vector contains the three elements: TRUE, FALSE and TRUE (in that order).

numeric_vector <- c(1, 10, 49)
character_vector <- c("a", "b", "c")

Complete the code for boolean_vector

boolean_vector <- c(TRUE, FALSE, TRUE)

Naming a vector

As a data analyst, it is important to have a clear view on the data that you are using. Understanding what each element refers to is therefore essential.

You can give a name to the elements of a vector with the names() function. Have a look at this example:

some_vector <- c("John Doe", "poker player")
names(some_vector) <- c("Name", "Profession")

This code first creates a vector some_vector and then gives the two elements a name. The first element is assigned the name Name, while the second element is labeled Profession. Printing the contents to the console yields following output:

      Name     Profession 
"John Doe" "poker player" 

Instructions

The code on the right names the elements in poker_vector with the days of the week. Add code to do the same thing for roulette_vector.

# Poker winnings from Monday to Friday
poker_vector <- c(140, -50, 20, -120, 240)

# Roulette winnings from Monday to Friday
roulette_vector <- c(-24, -50, 100, -350, 10)

# Assign days as names of poker_vector
names(poker_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

# Assign days as names of roulette_vectors
names(roulette_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

Some mathematical operation on vectors

It is important to know that if you sum two vectors in R, it takes the element-wise sum. For example, the following three statements are completely equivalent:

c(1, 2, 3) + c(4, 5, 6)
c(1 + 4, 2 + 5, 3 + 6)
c(5, 7, 9)

You can also do the calculations with variables that represent vectors:

a <- c(1, 2, 3) 
b <- c(4, 5, 6)
c <- a + b

Instructions

  • Take the sum of the variables A_vector and B_vector and it assign to total_vector.
  • Inspect the result by printing out total_vector.
A_vector <- c(1, 2, 3)
B_vector <- c(4, 5, 6)

Take the sum of A_vector and B_vector

total_vector <- A_vector + B_vector

Vector selection: the good times

In this section, our goal is to select specific elements of the vector. To select elements of a vector (and later matrices, data frames, …), you can use square brackets. Between the square brackets, you indicate what elements to select. For example, to select the first element of the vector, you type poker_vector[1]. To select the second element of the vector, you type poker_vector[2], etc. Notice that the first element in a vector has index 1, not 0 as in many other programming languages.

Selection by comparison

By making use of comparison operators, we can approach the previous question in a more proactive way.

The (logical) comparison operators known to R are:

  • < for less than
  • > for greater than
  • <= for less than or equal to
  • >= for greater than or equal to
  • == for equal to each other
  • != not equal to each other

As seen in the previous chapter, stating 6 > 5 returns TRUE. The nice thing about R is that you can use these comparison operators also on vectors. For example:

c(4, 5, 6) > 5
[1] FALSE FALSE TRUE

This command tests for every element of the vector if the condition stated by the comparison operator is TRUE or FALSE.

Instructions

Check which elements in poker_vector are positive (i.e. > 0) and assign this to selection_vector. Print out selection_vector so you can inspect it. The printout tells you whether you won (TRUE) or lost (FALSE) any money for each day.

Poker and roulette winnings from Monday to Friday:

poker_vector <- c(140, -50, 20, -120, 240)
roulette_vector <- c(-24, -50, 100, -350, 10)
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
names(poker_vector) <- days_vector
names(roulette_vector) <- days_vector

Which days did you make money on poker?

selection_vector <- poker_vector > 0

Matrices (Session 3)

In this session you will learn how to work with matrices in R. By the end of the chapter, you will be able to create matrices and to understand how you can do basic computations with them.

What’s a matrix?

In R, a matrix is a collection of elements of the same data type (numeric, character, or logical) arranged into a fixed number of rows and columns. Since you are only working with rows and columns, a matrix is called two-dimensional.

You can construct a matrix in R with the matrix() function. Consider the following example:

matrix(1:9, byrow = TRUE, nrow = 3)

In the matrix() function:

  • The first argument is the collection of elements that R will arrange into the rows and columns of the matrix. Here, we use 1:9 which is a shortcut for c(1, 2, 3, 4, 5, 6, 7, 8, 9).
  • The argument byrow indicates that the matrix is filled by the rows. If we want the matrix to be filled by the columns, we just place byrow = FALSE.
  • The third argument nrow indicates that the matrix should have three rows.

Instructions

Construct a matrix with 3 rows containing the numbers 1 up to 9, filled row-wise.

matrix(1:9, byrow = TRUE, nrow = 3)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9

Naming a matrix

To help you remember what is stored in star_wars_matrix, you would like to add the names of the movies for the rows. Not only does this help you to read the data, but it is also useful to select certain elements from the matrix.

Similar to vectors, you can add names for the rows and the columns of a matrix:

rownames(my_matrix) <- row_names_vector
colnames(my_matrix) <- col_names_vector

We went ahead and prepared two vectors for you: region, and titles. You will need these vectors to name the columns and rows of star_wars_matrix, respectively.

Instructions

  • Use colnames() to name the columns of star_wars_matrix with the region vector.
  • Use rownames() to name the rows of star_wars_matrix with the titles vector.
  • Print out star_wars_matrix to see the result of your work.

Box office Star Wars (in millions!)

new_hope <- c(460.998, 314.4)
empire_strikes <- c(290.475, 247.900)
return_jedi <- c(309.306, 165.8)

Construct matrix

star_wars_matrix <- matrix(c(new_hope, empire_strikes, return_jedi), nrow = 3, byrow = TRUE)

Vectors region and titles, used for naming

region <- c("US", "non-US")
titles <- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi")

Name the columns with region

colnames(star_wars_matrix) <- region

Name the rows with titles

rownames(star_wars_matrix) <- titles