R Fundamentals

About this chapter

  1. Questions:
  • How do I use R?
  1. Objectives:
  • Become familiar with R syntax
  • Understand the concepts of objects and assignment
  • Get exposed to a few functions
  1. Keypoints:
  • R’s capabilities are provided by functions
  • R users call functions and get results

Working with R

In this workshop we’ll use R in the extremely useful RStudio software For the most part we’ll work interactively, meaning we’ll type stuff straight into the R console in RStudio (Usually this is a window on the left or lower left) and get our results there too (usually in the consoled or in a window on the right). That’s what you see in panels like the ones below - first the thing to type into R, and below it, the calculated result from R. Let’s look at how R works by using it for it’s most basic job - as a calculator:

 3 + 5
[1] 8
 12 * 2
[1] 24
 1 / 3
[1] 0.3333333
 12 * 2
[1] 24
  3 / 0
[1] Inf

Fairly straightforward, we type in the expression and we get a result. That’s how this whole book will work, you type the stuff in, and get answers out. It’ll be easiest to learn if you go ahead and copy the examples one by one. Try to resist the urge to use copy and paste. Typing longhand really encourages you to look at what you’re entering.

As far as the R ouput itself goes, it’s really straightforward - its just the answer with a [1] stuck on the front. This [1] tells us how far through the output we are. Often R will return long lists of numbers and it can be helpful to have this extra information

Variables

We can save the output of operations for later use by giving it a name using the assignment symbol <-. Read this symbol as ‘gets’, so x <- 5 reads as ‘x gets 5’. These names are called variables, because the value they are associated with can change.

Let’s give five a name, x then refer to the value 5 by it’s name. We can then use the name in place of the value. In the jargon of computing we say we are assigning a value to a variable.

 x <- 5
 x
[1] 5
 x * 2
[1] 10
y <- 3
x * y
[1] 15

This is of course of limited value with just numbers but is of great value when we have large datasets, as the whole thing can be referred to by the variable.

Using objects and functions

At the top level, R is a simple language with two types of thing: functions and objects. As a user you will use functions to do stuff, and get back objects as an answer. Functions are easy to spot, they are a name followed by a pair of brackets like mean() is the function for calculating a mean. The options (or arguments) for the function go inside the brackets:

 sqrt(16)
[1] 4

Often the result from a function will be more complicated than a simple number object, often it will be a vector (simple list), like from the rnorm() function that returns lists of random numbers

 rnorm(100)
  [1] -0.01843255  0.06421873 -1.50053417 -0.79666852 -0.72308498  0.21275498
  [7] -0.90054663  0.20625956 -1.02446344 -2.39769508 -0.89480164 -0.54665639
 [13]  0.62959067  1.44304001  1.09709995 -0.75365803  2.59880385 -0.69080853
 [19]  0.51840751  0.39581426  0.63273984  1.38079089  0.71655540 -1.55506355
 [25] -0.31413327 -0.40700380  0.83764354 -0.95600448  0.70028794  1.21488145
 [31] -0.15479096 -1.42150496  1.03322586 -0.40359504 -0.10172240 -0.07047335
 [37]  0.63434296 -1.17270654  0.40538835 -1.06618095  1.39661204 -1.07512523
 [43] -0.44193934  0.13600539 -0.80022218 -0.20494628  0.80803373 -0.53831119
 [49]  1.10224338  0.75215216  0.63177346 -1.31442138 -0.11247949 -0.03266200
 [55]  0.90621141  0.80223313 -0.02449737  1.60551622 -1.55375866 -1.55498309
 [61]  0.58912504 -0.31439354 -0.96760550 -0.55283509 -0.35277784  0.39366931
 [67]  1.47995059 -1.65130946  0.81658990  0.92128496 -1.25964757 -1.04357169
 [73]  0.95935917 -0.02139596  1.73139961 -1.25691529  0.19655353 -1.29498514
 [79]  0.99813698  2.24970587  1.36267287 -1.49172876  0.26003621 -0.52192382
 [85]  0.40840595  0.34089138 -0.29227287 -0.65753492  2.21429686 -1.29418776
 [91]  0.41042976 -0.25250352  0.82187923  1.41168855  0.61378928 -1.11279085
 [97] -0.48434316  1.24987793  0.34168986  0.36798729

We can combine objects, variables and functions to do more complex stuff in R, here’s how we get the mean of 100 random numbers.

numbers <- rnorm(100)
mean(numbers)
[1] -0.1356049

Here we created a vector object with rnorm(100) and assigned it to the variable numbers. We than used the mean() function, passing it the variable numbers. The mean() function returned the mean of the hundred random numbers.

Bracket notation in this document

I’m going to use the following descriptions for the symbols (), [] and {}:

() are brackets, [] are square brackets {} are curly brackets

Quiz

  1. Create two variables, a and b: Add them. What happens if we change a and then re-add a and b?
  2. We can also assign a + b to a new variable, c. How would you do this?
  3. Try some R functions: round(), c(), range(), plot() hint: Get help on a function by typing ?function_name e.g ?c(). Use the mean() function to calculate the average age of everyone in your house (Invent a housemate if you have to).