In this section we download and install R and R Studio, and then show you how to write R commands and navigate around the RStudio interface. The goal in this chapter is not to learn any statistical or programming concepts: we’re just trying to learn how R works and get comfortable interacting with the system. We’ll spend a bit of time using R as a simple calculator. Specifically, we will learn the basics of R and RStudio, namely

  1. How to install R and RStudio interface
  2. How to navigate around the RStudio interface; a free Integrated Development Environment (IDE) for R
  3. How to install and load packages that provide extra functionality for R

1.1 Installing R & RStudio

An important distinction to remember is between the R programming language itself, and the software you use to interact with R. You could choose to interact with R directly from the terminal, but that’s painful, so most people use an integrated development environment (IDE), which takes care of a lot of boring tasks for you. To get started, make sure you have both R and RStudio installed on your computer. Both are free and open source, and for most people they should be straightforward to install.

1.1.1 Install XCode if you have a Mac

  • If you have a Mac make sure that before installing R and R studio you

    • upgrade to the latest version of macOS
    • install XCode through the appStore

1.1.2 Install R

First you need to install R itself (the engine). Go to the CRAN (Collective R Archive Network)– this is the site where R itself and most R packages live. Click on “Download R for XXX”, where XXX is either Mac or Windows:

Double click on the downloaded file. Click *Yes** through all the prompts to install like any other program. once finished, proceed to install R Studio.

1.1.3 Install RStudio IDE

Go to the R studio website, and follow the links to download. RStudio is a powerful user interface for programming in R. I suggest you install the preview version of R studio.

To get started, open the Rstudio application (i.e., RStudio.exe or RStudio.app), not the vanilla application (i.e., not R.exe or R.app). You should be looking at something like this:

The RStudio IDE is divided into 4 separate panes (one of which is hidden for now) which all serve specific functions. The Console starts with information about the R version number, license and contributors. The last line is a standard prompt > that indicates R is ready and expecting instructions to do something.

You edit scripts in the editor panel in R Studio and see results in the bottom right output panel.

For now, to make sure R and RStudio are setup correctly, type x <- 3 + 2 into the Console pane and execute it by pressing Enter/Return. You just created an object in R called x. What does this object contain? Type print(x) or just x into the console and press enter again. Your console should now contain the following output

x <- 3 + 2
print(x)
## [1] 5

Congratulations! You installed R and RStudio succesfully, created an object x to which you assigned the value 3+2 and managed to print the value of x

1.1.4 Change character encoding to UTF-8, and UTF-8 only

This may seem like an overly technical issue, but please bear with me. Since LBS is a very international school, we always seem to have issues with the language, or character encoding (Chinese, Arabic, Greek, Cyrillic, Hebrew, Thai, French, German, etc.), that people use in their computers. By default, all base R functions use the system native language encoding which has to do with the different languages some of us may have on our computers. Chinese and Greek users, having a completely different alphabet, typically report issues/problems/errors related to character encodings.

UTF-8 is the best possible character encoding, it works everywhere and we shall ask R Studio to use UTF-8 encoding globally. Please go to ToolsGlobal OptionsCodeSaving and and change the default text encoding to UTF-8 as shown below:

1.1.5 Exiting R & RStudio

When quitting RStudio you will be asked whether to Save workspace with two options:

  • Yes - Your current R workspace (containing the work that you have done) will be restored next time you open RStudio.
  • No - You will start with a fresh R session next time you open RStudio. For now select “No” to prevent errors being carried over from previous sessions.

In general, it’s good practice to always start with a fresh new session. If you want to do that, please go to ToolsGlobal Optionsand make sure that

  • Restore .RData into workspace at startup is NOT ticked
  • Save workspace to .RData on exit: select Never
  • Always save history (even when not saving .RData) is NOT ticked

as shown below

1.2 Updating R and RStudio

  • If you already installed R or RStudio for a previous course, update both to the most current version. Generally this entails downloading and installing the most recent version of both programs. When you update R, you don’t actually remove the old version - you have all versions on your computer and default to the most recent one. Sometimes this is useful when specific R libraries require an older version of R, however we will generally stick to the most recent versions of R and RStudio.
  • When you update R, make sure to update your packages as well. The following command should perform most of this work, update.packages(ask = FALSE, checkBuilt = TRUE) or you can go through the Packages tab in the bottom right panel of RStudio.

1.3 R commands

We have already seen how we can type commands in the command prompt and use R as a simple calculator. For instance, try typing 5 + 20, and hitting enter. When you do this, you’ve entered a command, and R will execute that command. What you see on screen now will be this:

5 + 20
## [1] 25

1.3.1 Assignmnent Operator <-

R treats everything (single numbers, lists, vectors, datasets) as objects. To create an object, we must use the assignment operator <-. For instance, if we had data on a student whose name is Alex, is 28 years old, and comes from Athens, we would create three objects, name, height, and city and assign the values of Alex, 28, and Athens respectively, we would type

name <- "Alex"
age <- 28
city <- "Athens"

The two objects have now been created; if we wanted to print out their values, we can use the print() function or just type the names of the objects.

print(name); print(age); print(city)
## [1] "Alex"
## [1] 28
## [1] "Athens"
name
## [1] "Alex"
age
## [1] 28
city
## [1] "Athens"

You can mentally read the command age <- 28 as object age becomes equal to the value 28. There is a keyboard shortcut Alt + - to get the assignment operator. We can do more interesting and useful things creating variables and assigning values to them. For instance, if we have the relevant dimensions and wanted to calculate the area and volume of a room, we could do it as follows:

room_length <- 5.63
room_width  <- 6.48
room_height <- 2.93
room_area <- room_length * room_width
room_volume <- room_length * room_width * room_height

room_area
## [1] 36.5
room_volume
## [1] 107

1.3.2 R is case sensitive

R is case sentitive and needs everything exactly as it was defined. age is different from AgE and Age. So if you type

age <- 28
AgE <- 34
Age <- 55

age; AgE; Age
## [1] 28
## [1] 34
## [1] 55

R will create three different objects.

1.3.3 Typos

R is a brilliant piece of software, but it cannot handle typos. Unlike Google’s search, “Did you mean…”, it takes it on faith that what you typed is exactly what you meant. For example, suppose that you forgot to hit the shift key when trying to type +, and as a result your command ended up being 5 = 20 rather than 5 + 20. Here’s what happens:

5 = 20
## Error in 5 = 20: invalid (do_set) left-hand side to assignment

R attempted to interpret 5 = 20 as a command, and spits out an error message because this makes no sense to it. Even more subtle is the fact that some typos won’t produce errors at all, because they happen to correspond to R commands. For instance, suppose that instead of 5 + 20, I mistakenly type command 5 - 20. Clearly, R has no way of knowing that you meant to add 20 to 5, not subtract 20 from 5, so what happens this time is this:

5 - 20
## [1] -15

In this case, R produces the right answer, but to the the wrong question.

R will always try to do exactly what you ask it to do. There is no autocorrect or equivalent to “Did you mean..” in R, and for good reason. When doing advanced stuff and even the simplest of statistics is pretty advanced in a lot of ways, it’s dangerous to let a mindless automaton like R try to overrule the human user. But because of this, it’s your responsibility to be careful. Always make sure you type exactly what you mean. When dealing with computers, it’s not enough to type approximately the right thing. In general, you absolutely must be precise in what you say to R … like all machines it is too stupid to be anything other than absurdly literal in its interpretation.

1.3.4 Comments

It is useful to put comments in your code, to make everything more readable. These comments could help others and you when you go back to your code in the future. R comments start with a hashtag sign #. Everything after the hashtag to the end of the line will be ignored by R. RStudio by default thinks that every line you write is a command; if you want to turn a line into a comment, place the cursor in the line and hit Ctrl + Shift + C in Windows or Cmd + Shift + C in a Mac.

# This line is a comment and will be ignored when run.
city # Text after the hashtag "#" is also ignored.
## [1] "Athens"

1.3.5 R knows you’re not finished

If you hit enter in a situation where it’s obvious to R that you haven’t actually finished typing the command, R is just smart enough to keep waiting. For example, if you wanted to calculate 15 - 4, and start by typing type 15 - and then press enter by mistake, R is smart enough to realise that you probably wanted to type in another number. So here’s what happens:

> 15 -
+

and there’s a blinking cursor next to the plus + sign. What this means is that R is still waiting for you to finish. It thinks you’re still typing your command, so it hasn’t tried to execute it yet. In other words, this plus sign is actually another command prompt. It’s different from the usual one (i.e., the > symbol) to remind you that R is going to add whatever you type now to what you typed last time. For example, if I then go on to type 4 and hit enter, what we get:

> 15 -
+ 4
[1] 11

And as far as R is concerned, this is exactly the same as if you had typed 15 - 4.

By the way, if after entering the 15 - you wanted to stop execution and cancel your command, just hit the escape key. R will return you to the normal command prompt (i.e. >) without attempting to execute the botched command.

1.3.6 Arithmetic Operations and Functions

R has the basic operators and you can use it as as simple calculator: addition is +, subtraction is -, multiplication is *, division is /, and ^ is the power operator:

2 + 3 
## [1] 5
5 - 8
## [1] -3
13 * 21
## [1] 273
34 / 55
## [1] 0.618
(5 * 13)/4 - 7
## [1] 9.25
# ^ : to the power off
2^3
## [1] 8
# square root
sqrt(25)
## [1] 5

Besides the basic operations functions, you can use standard mathematical functions

  • Rounding -round(), floor(), ceiling(),
  • Logarithms and Exponentials -exp(), log(), log10(), log2()
# R knows pi = 3.1415926...

# round to 2 decimal places 
round(pi, digits = 2); round(pi,2)
## [1] 3.14
## [1] 3.14
#Round down to nearest interger
floor(pi)
## [1] 3
#Round up to nearest interger
ceiling(pi)
## [1] 4

1.4 RStudio help

At this stage you know how to type in basic commands, including how to use some R functions. Few analysts bother to try to know or remember all the commands. What they really do is use tricks to make their lives easier. The first (and arguably most important one) is to use the internet. If you don’t know how a particular R function works, Google it. There is a lot of R documentation out there, and almost all of it is searchable! For the moment though, I want to call your attention to a couple of simple tricks that Rstudio makes available to you.

1.4.1 Tab autocomplete

The first thing I want to call your attention to is the autocomplete ability in Rstudio. Assume that what you want to do is to round a number. This time around, start typing the name of the function that you want, and then hit the Tab key. Rstudio will then display a little window like the one shown here:

In this figure, we have typed the letters rou at the command line, and then hit tab. The window has two panels. On the left, there’s a list of variables and functions that start with the letters typed shown in black text, and some grey text that tells you where that variable/function is stored. In our case, round is included in the {base} R, what is included in every new installation of R. There’s a few options there, and the one we want is round, but if you’re typing this yourself you’ll notice that when you hit the tab key the window pops up with the top entry highlighted. You can use the up and down arrow keys to select the one that you want. Or, if none of the options look right to you, you can hit the escape key (ESC) or the left arrow key to make the window go away.

In our case, the thing we want is the round option, and the panel on the right tells you a bit about how the function works. This display is really handy. The very first thing it says is round(x, digits = 0): what this is telling you is that the round function has two arguments. The first argument is called x, and it doesn’t have a default value. The second argument is digits, and it has a default value of 0. In a lot of situations, that’s all the information you need. But Rstudio goes a bit further, and provides some additional information about the function underneath. Sometimes that additional information is very helpful, sometimes it’s not: Rstudio pulls that text from the R help documentation, and my experience is that the helpfulness of that documentation varies wildly. Anyway, if you’ve decided that round is the function that you want to use, you can hit the enter key and Rstudio will finish typing the rest of the function name for you.

1.4.2 The history pane

One thing R does is keep track of your *command history**, i.e., it remembers all the commands previously typed. You can access this history in a few different ways. The simplest way is to use the up and down arrow keys. If you hit the up key, the R console will show you the most recent command that you’ve typed. Hit it again, and it will show you the command before that. If you want the text on the screen to go away, hit escape. Using the up and down keys can be handy if you’ve typed a long command that had one typo in it. Rather than having to type it again from scratch, you can use the up key to bring up the command and fix it.

The second way to get access to your command history is to look at the history panel in Rstudio. On the upper right panel of the Rstudio window, you’ll see a tab labelled History.

Click on that, and you’ ll see a list of all your recent commands displayed in that panel– double click on one of the commands, and it will be copied to the R console. You can achieve the same result by selecting the command you want with the mouse and then clicking the *“To Console”** button.

1.5 More resources

1.6 Acknowledgements



This page last updated on: 2020-07-14