1 R and RStudio

1.1 What’s the difference?

R is a programming language that runs computations while RStudio is an integrated development environment (IDE) - an interface for working in R with many convenient features and tools.

You can think of the two like this:

  • R is like a car’s engine.
  • RStudio is like a car’s dashboard.
R: Engine RStudio: Dashboard

Your car needs an engine (R) to run, but having a speedometer and rear view mirrors (RStudio) makes driving a lot easier.

1.2 Installing R and RStudio

You will first need to download and install both R and RStudio (Desktop version) on your computer.

  1. Download and install R.
  2. Download and install RStudio (Desktop version).

1.3 Using R via RStudio

After you install R and RStudio on your computer, you’ll have two new applications you can open. We will always work in RStudio. In other words:

R: Do not open this RStudio: Open this

After you open RStudio, you should see the following:

Notice the default panes:

  • Console (entire left)
  • Environment/History (tabbed in upper right)
  • Files/Plots/Packages/Help (tabbed in lower right)

FYI: you can change the default location of the panes, among many other things: Customizing RStudio.

Go into the Console on the left with the > (that’s the command prompt), and let’s get started!


2 R as a calculator

At its core, R is a calculator. You can use arithmetic, relational, and logical operators to evaluate a wide variety of computations.

2.1 Arithmetic operators

R can handle simple arithmetic using the following arithmetic operators:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Powers: ^
  • Integer division: %/%
  • Modulus: %%

The first four basic operators (+, -, *, /) are pretty straightforward and behave as expected:

7 + 5 # Addition
## [1] 12
7 - 5 # Subtraction
## [1] 2
7 * 5 # Multiplication
## [1] 35
7 / 5 # Division
## [1] 1.4

Not a lot of surprises (you can ignore the [1] you see in the returned values…that’s just R saying there’s only one value to return). The other three arithmetic operators (^, %/%, %%) are a little more subtle, so let’s examine them more closely.

Powers:

Powers (i.e. \(x^n\)) are represented using the ^ symbol. For example, to calculate \(5^4\) in R, we would type:

5^4
## [1] 625

Integer division:

Integer division is division in which the remainder is discarded. Note the difference between regular (/) and integer (%/%) division:

4 / 3 # Regular division
## [1] 1.333333
4 %/% 3 # Integer division
## [1] 1

With integer division, 3 can only go into 4 once, so 4 %/% 3 returns 1. If you try to integer divide a number by a larger number, you’ll get 0:

4 %/% 5 # Will return 0
## [1] 0

Modulus operator:

The modulus (aka “mod” operator) returns the remainder after doing integer division. For example:

17 %% 3
## [1] 2

This returns 2 because because 17 / 3 is equal to 5 with a remainder of 2. The modulus returns any remainder, including decimals:

3.1415 %% 3
## [1] 0.1415

If you mod a number by itself, you’ll get 0 (because there’s no remainder):

17 %% 17 # Will return 0
## [1] 0

Finally, if you mod a number by a larger number, you’ll get the smaller number back since it’s the remainder:

17 %% 20 # Will return 17
## [1] 17

2.2 Relational operators

R assesses whether a comparison of two values is TRUE or FALSE using the following relational operators:

  • Less than: <
  • Less than or equal to : <=
  • Greater than or equal to: >=
  • Greater than: >
  • Equal: ==
  • Not equal: !=

The less than operator < can be used to test whether one number is smaller than another number:

2 < 5
## [1] TRUE

If the two values are equal, the < operator will return FALSE, while the <= operator will return TRUE: :

2 < 2
## [1] FALSE
2 <= 2
## [1] TRUE

The “greater than” (>) and “greater than or equal to” (>=) operators work the same way but in reverse:

2 > 5
## [1] FALSE
2 > 2
## [1] FALSE
2 >= 2
## [1] TRUE

To assess whether two values are equal, we have to use a double equal sign (==):

(2 + 2) == 4
## [1] TRUE
(2 + 2) == 5
## [1] FALSE

To assess whether two values are not equal, we have to use an exclamation point sign with an equal sign (!=):

(2 + 2) != 4
## [1] FALSE
(2 + 2) != 5
## [1] TRUE

It’s worth noting that you can also apply equality operations to strings. R understands that a "penguin" is a "penguin" so you get this:

"penguin" == "penguin"
## [1] TRUE

However, R is very particular about what counts as equality. For two pieces of text to be equal, they must be precisely the same:

"penguin" == "PENGUIN"        # Returns FALSE
"penguin" == "p e n g u i n"  # Returns FALSE
"penguin" == "penguin "       # Returns FALSE
## [1] FALSE
## [1] FALSE
## [1] FALSE

2.3 Logical operators

R assesses logical statements using the following logical operators:

  • And: &
  • Or: |
  • Not: !

And:

A logical expression x & y is TRUE only if both x and y are TRUE.

(2 == 2) & (2 == 3)
## [1] FALSE
(2 == 2) & (3 == 3)
## [1] TRUE

Or:

A logical expression x | y is TRUE if either x or y are TRUE.

(2 == 2) | (2 == 3)
## [1] TRUE

Not:

The ! operator behaves like the word “not” in everyday language. If a statement is “not true”, then it must be “false”. For example:

!TRUE
## [1] FALSE

In practice, it is often helpful to include parentheses to clarify the statement being made. Consider the following example:

!3 == 5
## [1] TRUE

This returns TRUE, but it’s a bit confusing. Reading from left to right, you start by saying “not 3”…what does that mean?

What is really going on here is R first evaluates whether 3 is equal to 5 (3 == 5), and then returns the “not” (!) of that. A better version of the same thing would be:

!(3 == 5)
## [1] TRUE

2.4 Order of operations

R follows the typical BEDMAS order of operations. That is, you first calculate things inside Brackets, then calculate Exponents, then Division and Multiplication, then Addition and Subtraction. For a more precise statement, see the operator precedence for R. For example, if I type:

1 + 2 * 4
## [1] 9

R first computes 2 * 4 and then adds 1. If what you actually wanted was for R to first add 2 to 1, then you should have added parentheses around 1 and 2:

(1 + 2) * 4
## [1] 12

A helpful rule of thumb to remember is that brackets always come first. So, if you’re ever unsure about what order R will do things in, an easy solution is to enclose the thing you want it to do first in brackets.

2.5 R ignores excess spacing

When I typed 10 + 20 before, I could equally have done this

10        + 20
## [1] 30

or this

            10   + 20
## [1] 30

and get exactly the same answer.

2.6 Using comments

In R, the # symbol is a special symbol that denotes a comment. R will ignore anything on the same line that follows the # symbol. This enables us to write comments around our code to explain what we’re doing:

2 + 2 # This is a comment explaining that I'm adding two numbers
## [1] 4

Notice that R ignores the whole sentence after the # symbol.


3 Staying organized

3.1 The history pane

R keeps track of your “command history”. 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.

The second way to get access to your command history is to look at the history panel in Rstudio. On the upper right hand side 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. It should look something like this:

If you double click on one of the commands, it will be copied to the R console.

3.2 Working directory

Any process running on your computer has a notion of its “working directory”. In R, this is where R will look for files you ask it to load. It’s also where any files you write to disk will go.

You can explicitly check your working directory with:

getwd()

It is also displayed at the top of the RStudio console.

As a beginning R user, it’s OK let your home directory or any other weird directory on your computer be R’s working directory. Very soon, I urge you to evolve to the next level, where you organize your analytical projects into directories and, when working on project A, set R’s working directory to the associated directory.

Although I do not recommend it, in case you’re curious, you can set R’s working directory at the command line like so:

setwd("~/myCoolProject")

Although I do not recommend it, you can also use RStudio’s Files pane to navigate to a directory and then set it as working directory from the menu:

Session > Set Working Directory > To Files Pane Location.

You’ll see even more options there). Or within the Files pane, choose More and Set As Working Directory.

But there’s a better way. A way that also puts you on the path to managing your R work like an expert.

3.3 RStudio projects

Keeping all the files associated with a project organized together – input data, R scripts, analytical results, figures – is such a wise and common practice that RStudio has built-in support for this via its projects.

Using Projects

Let’s make one for practice. Do this:

File > New Project ….

You should see the following pane:

Choose “New Directory”. The directory name you choose here will be the project name. Call it whatever you want. RStudio will create a folder with that name to put all your project files.

As a demo, I created a project on my Desktop called “demo”. RStudio created a new project called “demo”, and in this folder there is a file called “demo.Rproj”. If I double-click on this file, RStudio will open up, and my working directory will be automatically set to this folder! You can double check this by typing:

getwd()

3.4 Save your code in .R Files

It is traditional to save R scripts with a .R or .r suffix. Any code you wish to re-run again later should be saved in this way and stored within your project folder. For example, if you wanted to re-run all of the code in this tutorial, open a new .R file and save it to your R project folder. Do this:

File > New File > R Script

Copy-paste all of the code we’ve typed so far into this file:

10 + 20
5^4
4 / 3 # Regular division
4 %/% 3 # Integer division
17 %% 3
3.1415 %% 3
1 + 2 * 4
(1 + 2) * 4
10        + 20
10   + 20
2 + 2 # This is a comment explaining that I'm adding two numbers
getwd()

Then save this new R script with some name. Do this:

File > Save

I called the file “tutorial.R” and saved it in my R project folder called “demo”.

Now when I open the “demo.Rproj” file, I see in my files pane the “tutorial.R” code script. I can click on that file and continue editing it!

I can also run any line in the script by typing “Command + Enter” (Mac) or “Control + Enter” (Windows).


4 Tips

4.1 “Chopping” up numbers with %% and %/% of 10s

When you use the mod operator (%%) on a positive number with factors of 10, it “chops” a number at a particular digit and returns everything to the right. For example:

123456 %% 1 # Chops to the right of the ones digit
## [1] 0
123456 %% 10 # Chops to the right of the tens digit
## [1] 6
123456 %% 100 # Chops to the right of the hundreds digit
## [1] 56

Integer division (%/%) does the opposite - it returns everything to the left of the “chop” point. For example:

123456 %/% 1 # "Chops to the right of the ones digit
## [1] 123456
123456 %/% 10 # "Chops to the right of the tens digit
## [1] 12345
123456 %/% 100 # "Chops to the right of the hundreds digit
## [1] 1234

This works with non-integers too:

3.1415 %% 1
## [1] 0.1415
3.1415 %/% 1
## [1] 3

But be careful - this “trick” only works with positive numbers:

-123.456 %% 10
## [1] 6.544
-123.456 %/% 10
## [1] -13

Here’s some mental notes to remember how this works:

  • %% returns everything to the right (<chop> ->)
  • %/% returns everything to the left (<- <chop>)
  • The “chop” point is always just to the right of the chopping digit:
Example “Chop” point “Chop” point description
1234 %% 1 1234 | Right of the 1’s digit
1234 %% 10 123 | 4 Right of the 10’s digit
1234 %% 100 12 | 34 Right of the 100’s digit
1234 %% 1000 1 | 234 Right of the 1,000’s digit
1234 %% 10000 | 1234 Right of the 10,000’s digit

4.2 Odds and evens with %%

You can tell if an integer is even or odd by using mod 2. If you get 0, it’s even; if you 1, it’s odd:

10 %% 2 # Even
## [1] 0
11 %% 2 # Odd
## [1] 1
12 %% 2 # Even
## [1] 0
13 %% 2 # Odd
## [1] 1

Page sources:

Some content on this page has been modified from other courses, including:


EMSE 6574, Sec. 11: Programming for Analytics (Fall 2019)
George Washington University | School of Engineering & Applied Science
Dr. John Paul Helveston | jph@gwu.edu | Mondays | 6:10–8:40 PM | Phillips Hall 108 | |
This work is licensed under a Creative Commons Attribution 4.0 International License.
See the licensing page for more details about copyright information.
Content 2019 John Paul Helveston