Welcome to your first session on how to use R. We will have several more sessions like this. Afterwards, you will have a better understanding of processing cultural and social datasets with R.

Let’s try to add two numbers. Please, type 4 + 3 into the command prompt. Then, press enter.

4 + 3
## [1] 7

Now, let’s try 12 – 5.

12 - 5
## [1] 7

And, finally, brackets can be used to indicate that operators in the sub-expression take precedence. Try (5 * 3) – 4.

(5 * 3) - 4
## [1] 11

These were all numbers. All this is very similar to what you know from calculators. But computers can process many other symbols as well. Other important symbols include ‘strings’, which you know as words. These can be any number of characters like a, b, c, etc. but also -, 1 or & count as characters.

Let’s try this. Please, type ‘Hello World’ into the command prompt. The quotation marks ’’ indicate that this is a string. You could also use “Hello World”.

'Hello World'
## [1] 'Hello World'

Let’s repeat. Please, type ‘From KCL Digital Humanities’.

'From KCL Digital Humanities'
## [1] 'From KCL Digital Humanities'

What do you see when you type typeof(‘string’)?

typeof('string')
## [1] 'character'

There are many more types, which we will cover throughout the course. Booleans are another important one. They evaluate whether an assumption is TRUE or FALSE. Please, type 4 < 5. What do you get back?

4 < 5
## [1] TRUE

Another important concept that discriminates programming languages from calculators are variables. They are basically names for places in the computer’s memory where we can store things. We create variables with the R assignment operator <-. You can also use = in R.

Let’s try that and assign the value 5 to the variable my_apples. Please, type my_apples <- 5.

my_apples <- 5

Well done. Now print out my_apples. Just type my_apples into the command prompt.

my_apples
## [1] 5

Now let’s try to assign two variables. First type my_apples <- 5

my_apples <- 5

Now type my_oranges <- 6. You have now created two variables my_apples and my_oranges.

my_oranges <- 6

Just like numbers we can add two numerical variables. Please try it with my_apples + my_oranges.

my_apples + my_oranges
## [1] 11

We can also assign the result to a new variable my_fruit. Please type my_fruit <- my_apples + my_oranges.

my_fruit <- my_apples + my_oranges

To check that the new variable exists, please enter my_fruit.

my_fruit
## [1] 11

But we can only combine variables of the same type. Please assign the string ‘six’ to my_oranges with my_oranges <- ‘six’.

my_oranges <- 'six'

Now let’s try and ‘add’ my_apples and my_oranges. Type my_apples + my_oranges and you will get an error. Afterwards, type skip() to move to the next question.

my_apples + my_oranges
## [1] Error in my_apples + my_oranges : non-numeric argument to binary operator

Variables are very important in any programming language. Another key idea is the function. It is basically a predefined set of commands, which you give your own name. On your calculator you can, for instance, use the inv function to get the inverse of a number. R also has a lot of pre-defined functions like inv. But in any programming language you can also define your own functions. We will come back to this later.

In R functions are called with arguments in brackets. Please type in sqrt(9) to get the square root of 9. sqrt is the function name and 9 is the only argument. BTW, when you typed swirl() to start SWIRL you have called the swirl function. This function has no arguments and thus only empty brackets.

sqrt(9)
## [1] 3

A function can also have more than one argument. In order to sum 1,3 and 5, type in sum(1, 3, 5). We have now called sum with three arguments.

sum(1, 3, 5)
## [1] 9

Some functions have arguments with different types. For instance, try rep(‘Hello World!’, times = 3) to print out Hello World three times. The first argument is a string and the second a number.

rep('Hello World!', times = 3)
## [1] "Hello World!" "Hello World!" "Hello World!"

This was a lot of stuff for the first lesson. Unfortunately, this is necessary but once you have learned one programming language all of this becomes quite obvious and repetitive. Before we finish we need to learn one more important concept that is specific to R. With so-called vectors, you can collect several elements in the same variable. This is immensely useful as we see later.

Let’s try vectors, which store an ordered set of values called elements. A vector can contain any number of elements using the combine function c(,,_). Type in numeric_vector <- c(1, 10, 49) to create a numeric vector of three numbers and then print it out. Please, be aware that vectors can only contain elements of the same type. In this case numbers.

numeric_vector <- c(1, 10, 49)

To check that the new vector exists, please type numeric_vector.

numeric_vector
## [1]  1 10 49

We can also create string/characters vectors, with character_vector <- c(‘abc’, ‘def’, ‘ghi’).

character_vector <- c('abc', 'def', 'ghi')

To check that the new vector exists, please type character_vector.

character_vector
## [1] "abc" "def" "ghi"

As said, vectors can only contain one type. Try c(1,2,‘abc’) and you can see how elements are forced into the same type.

c(1,2,'abc')
## [1] "1"   "2"   "abc"

Vectors are useful to, for instance, hold your poker winnings during the week. You do not play on Saturday and Sunday, but record all the other 5 days, by entering poker_vector <- c(140, -50, 20, -120, 240). Thank you datacamp.com for this example! An excellent resource to learn R btw, but unfortunately you have to pay for it.

poker_vector <- c(140, -50, 20, -120, 240)

You feel this is your lucky week. So, you play roulette, too. Please record your winnings with roulette_vector <- c(-24, -50, 100, -350, 10).

roulette_vector <- c(-24, -50, 100, -350, 10)

Because you prefer it organised, you would now like to name each of the entries. This is possible in R with another function called names. Please type in names(poker_vector) <- c(“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”).

names(poker_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

And the same for the roulette winnings. Run names(roulette_vector) <- c(“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”).

names(roulette_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

And now for real magic. Just like numbers, you can add also numbers in vectors. In order to perform an element-wise vector addition, please try total_vector <- poker_vector + roulette_vector.

total_vector <- poker_vector + roulette_vector

Have a look at the new vector with total_vector.

total_vector
##    Monday   Tuesday Wednesday  Thursday    Friday
##       116      -100       120      -470       250

total_vector shows your daily winnings. Well, done. You can also add, subtract, multiply or divide any number from your vector. Try total_vector + 100.

total_vector + 100
##    Monday   Tuesday Wednesday  Thursday    Friday
##       216         0       220      -370       350

Now try total_vector / 100.

total_vector / 100
##    Monday   Tuesday Wednesday  Thursday    Friday
##      1.16     -1.00      1.20     -4.70      2.50

Next, you are interested in how much you win in poker and roulette per day. You can simply add up all the elements in the vectors. Remember that we can use built-in functions like sqrt? To add up elements in a vector we can use the function sum. Try it with sum(poker_vector).

sum(poker_vector)
## [1] 230

And of course we can also do sum(roulette_vector).

sum(roulette_vector)
## [1] -314

In order to get our total winnings per day for the week, we can simply add both vectors with the R sum function. This adds each element in the first vector to its corresponding element in the second. Try it with total_week <- sum(poker_vector + roulette_vector).

total_week <- sum(poker_vector + roulette_vector)

Print out total_week.

total_week
## [1] -84

We have almost covered everything there is to know about vectors. One more important concept is that vectors are indexed. Using square brackets, we can select the first, second and third element directly with 1, 2, 3, etc. respectively. To select Monday’s poker winning simply use square brackets [] and type poker_monday <- poker_vector[1].

poker_monday <- poker_vector[1]

Print out poker_monday.

poker_monday
## Monday
##    140

You can also select more than one element with the colon operator. In order to select your winnings from Tuesday to Friday, please run roulette_selection_vector <- roulette_vector[2:5].

roulette_selection_vector <- roulette_vector[2:5]

To find out about our roulette winnings from Tuesday to Friday, we can use sum again. Try sum(roulette_selection_vector).

sum(roulette_selection_vector)
## [1] -290

Using the square brackets we can also update elements of a vector. Let’s remove your Wednesday’s winnings and set the corresponding value to 0 with total_vector[3] <- 0.

total_vector[3] <- 0

Print out total_vector.

total_vector
##    Monday   Tuesday Wednesday  Thursday    Friday
##       116      -100         0      -470       250

Let’s also set the Wednesday winnings of roulette_selection_vector to 1000 with roulette_selection_vector[2] <- 1000.

roulette_selection_vector[2] <- 1000

Print out roulette_selection_vector.

roulette_selection_vector
##   Tuesday Wednesday  Thursday    Friday
##       -50      1000      -350        10

Vectors can also represent functions. Type in first x <- seq(1, 20, 0.1). Ask your friend the Internet what seq does!

x <- seq(1, 20, 0.1)

Print out x.

x
##   [1]  1.0  1.1  1.2  1.3  1.4  1.5  1.6  1.7  1.8  1.9  2.0  2.1  2.2  2.3
##  [15]  2.4  2.5  2.6  2.7  2.8  2.9  3.0  3.1  3.2  3.3  3.4  3.5  3.6  3.7
##  [29]  3.8  3.9  4.0  4.1  4.2  4.3  4.4  4.5  4.6  4.7  4.8  4.9  5.0  5.1
##  [43]  5.2  5.3  5.4  5.5  5.6  5.7  5.8  5.9  6.0  6.1  6.2  6.3  6.4  6.5
##  [57]  6.6  6.7  6.8  6.9  7.0  7.1  7.2  7.3  7.4  7.5  7.6  7.7  7.8  7.9
##  [71]  8.0  8.1  8.2  8.3  8.4  8.5  8.6  8.7  8.8  8.9  9.0  9.1  9.2  9.3
##  [85]  9.4  9.5  9.6  9.7  9.8  9.9 10.0 10.1 10.2 10.3 10.4 10.5 10.6 10.7
##  [99] 10.8 10.9 11.0 11.1 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 12.0 12.1
## [113] 12.2 12.3 12.4 12.5 12.6 12.7 12.8 12.9 13.0 13.1 13.2 13.3 13.4 13.5
## [127] 13.6 13.7 13.8 13.9 14.0 14.1 14.2 14.3 14.4 14.5 14.6 14.7 14.8 14.9
## [141] 15.0 15.1 15.2 15.3 15.4 15.5 15.6 15.7 15.8 15.9 16.0 16.1 16.2 16.3
## [155] 16.4 16.5 16.6 16.7 16.8 16.9 17.0 17.1 17.2 17.3 17.4 17.5 17.6 17.7
## [169] 17.8 17.9 18.0 18.1 18.2 18.3 18.4 18.5 18.6 18.7 18.8 18.9 19.0 19.1
## [183] 19.2 19.3 19.4 19.5 19.6 19.7 19.8 19.9 20.0

Type in first y <- sin(x). Ask your friend the Internet what sin does!

y <- sin(x)

Print out y.

TRUE
## [1] TRUE

One of the major strengths of R is its graphing ability, which we will cover in much more detail later. Please plot the sin function with plot(x,y).

plot(x,y)

Let’s check what we have learned already about programming and go through a couple of questions.

If you enter (12 - 11) * 100, what is the output?

  1. 100
  2. 1
  3. 50
  4. 2

100

What is a vector in R?

  1. A new life form
  2. A geometric object that has magnitude (or length) and direction
  3. An ordered set of values called elements
  4. My middle name

An ordered set of values called elements

If you want to add up a vector c(1,2,3), which function can you use?

  1. x <- c(1,2,3)
  2. add(c(1,2,3))
  3. sum(c(‘1’,‘2’,‘3’))
  4. sum(c(1,2,3))

sum(c(1,2,3))

If you are still motivated, there is something you could do in your working group. Try and take turn to explain the ideas behind variables, types and functions in R to each other.

Let’s move on to the second part of today.