Basic Building Blocks

Getting Started with R

In this lesson, we will explore some basic building blocks of the R programming language.

If at any point you’d like more information on a particular topic related to R, you can type help.start() at the prompt, which will open a menu of resources (either within RStudio or your default web browser, depending on your setup). Alternatively, a simple web search often yields the answer you’re looking for.

How to use R as a calculator

In its simplest form, R can be used as an interactive calculator. Type 5 + 7 and press Enter.

5 + 7
## [1] 12

R simply prints the result of 12 by default. Let’s try that again with some different numbers and operations.

As well as addition (+), we can do substraction. Type 5 - 7 and press Enter

5 - 7
## [1] -2

What about division? Type 5 / 7 and press Enter

5 / 7
## [1] 0.7142857

Or multiplication? There is no ‘x’ to indicate ‘multiply by’ as you probably learnt at school. Instead, most computer languages use ’*’. Type 5 * 7 and press Enter

5 * 7
## [1] 35

We are not limited to two numbers … we can use as many as you like. Type 8 + 2 - 10 / 43 and press Enter

8 + 2 - 10 / 43
## [1] 9.767442

These operations follow the general laws of arithmetic (e.g. multiplication and division first, followed by addition and subtraction).

How to assign variables and use R for algebra

This arithmetic is all very useful, but only slighty easier than using a calculator! However, R is a programming language and often the reason we use a programming language as opposed to a calculator is to automate some process or avoid unnecessary repetition.

In this case, we may want to use our result from above in a second calculation. Instead of retyping 5 + 7 every time we need it, we can just create a new variable that stores the result.

The way you assign a value to a variable in R is by using the assignment operator, which is just a ‘’less than’‘symbol followed by a’‘minus’’ sign. It looks like this: <-

Think of the assignment operator as an arrow. You are assigning the value on the right side of the arrow to the variable name on the left side of the arrow.

To assign the result of 5 + 7 to a new variable called x, you type x <- 5 + 7. This can be read as ‘x gets 5 plus 7’. Give it a try now. What happened?!

x <- 5 + 7

You’ll notice that R did not print the result of 12 this time. When you use the assignment operator, R assumes that you don’t want to see the result immediately, but rather that you intend to use the result for something else later on. R stores the variable x and its contents in the memory for this R session.

To view the contents of the variable x, just type x and press Enter. Try it now.

x
## [1] 12

This ability to store variables, or objects, and their contents is very useful. It allows us to pass these variables to other calculations, as well as store the output as a different variable. Note that you can also check the value of your variables in your “global environment” window, normally in the upper right of your screen.

Now, store the result of x - 3 in a new variable called y.

y <- x - 3

What is the value of y? Type y to find out.

y
## [1] 9

However, we don’t always work with just numbers in R. For example, we can also work with characters that represent text! Anything can be converted into a character in R by surrounding it with quotes, for example “text”.

Try re-assigning the variable x to give it the word ‘hello’.

x <- "hello"

Lets look at the variable. Call your x variable now (i.e., type x).

x
## [1] "hello"

Isn’t that nice. Now assign the variable y to the character “2”.

y <- "2"

Lets check what type of variable y is. We can do with the useful class() function. Enter class(y).

class(y)
## [1] "character"

This function is telling you that y is a character, not a number. This is the result of our quotation marks! If you tried to perform a mathematical operation on y, like + or -, R would give an error. You must be careful with your variable types in R—characters don’t play well with numbers for example, and there are other more subtle situations we’ll get to where this can be problematic.

Moving around inside your computer

When you work with your computer, most of the time I expect it has been with a Graphical User Interface (or GUI). This way is a very easy and intuitive way of interacting with a computer. You can open folders (directories) and files and view them, move folders and files around, etc, etc.

Doing similar tasks with the command-line is a tiny bit more complex, but still the same idea. The first thing we want to know when we start RStudio is where exactly in the computer we are.

RStudio has a default directory where it always starts as looking at. Usually this is your user home directory. You can check this using the function getwd(). Try that now.

getwd()
## [1] "/home/smo/Dropbox/Teaching/2018-2019/2018-720-R/draft/swirl/2018-09-10"

‘wd’ stands for ‘working diretory’. Typing getwd() will return a PATH. This is the hierarchical ‘address’ of the folder. It will usually start at the drive (often ‘C:’ in windows) and go down and down to get to your user directory.

You can change the default directory for RStudio by going to Tools > global options. But, there is really no need to. You can easily change the working directory for each project you want to work on using the function setwd() and specifying a PATH to the folder you want.

We will come back to using directories and PATHs later in the course when we start reading and writing data.

You have now learned how to do basic operations in R, assign variables, and learned the difference between two common variable types. You are on your way to becoming an R wizard!

Please submit the log of this lesson to Google Forms so that Simon may evaluate your progress.

  1. Sure, no problem

Sure, no problem