Introduction To R
Introduction To R
Introduction to R
Laura Brannelly, Saras Windecker, Patrick Baker & Kaitlyn Hammond
26 February 2022
What is R?
R is an open-source statistical software program that has gained rapid popularity because of its power and
flexibility. One of R’s greatest assets is the large number of “packages” that have been written by R-users and
are freely available from CRAN (http://cran.us.r-project.org/) (the Comprehensive R Archive Network). They
contain all of the classical statistical tests that you will likely ever need.
What is RStudio?
RStudio is a user interface for R that creates a flexible environment for coding and visualizing data. It is free
and can be downloaded locally to your computer or can be accessed through a subscription to RStudio Cloud.
This semester, we will be using the online cloud version of RStudio to help ensure everyone is able to access
the packages we will need for our data analyses. If you are also interested in running R on your personal
computer, we will provide instructions for how to do so later in the semester.
RStudio Cloud works in a system called projects. This is a convenient way to ensure you can find your data, R
scripts, and any R outputs. RStudio Cloud projects save automatically, so when you leave and come back to
your project it will be exactly how you left it!
To begin the prac, simply click the start button on the assignment. This will deploy a personal copy of the
project for you to work in.
This is the RStudio workspace. Your window will contain three panes. On the left-hand side is your R console.
This is where you can type and run your code. In the upper right is your environment pane. When you begin to
use R, this will contain any objects or tables you have assigned so that you can see and access them. On the
bottom right is the files pane. Here you can see the structure of the project. There are three folders: Data ,
Manual , and Scripts .
The Data folder will contain any data you will need for the prac. The Manual folder will contain the html file of
the prac manual (the document you are currently reading!) for that particular prac. The Scripts folder can be
used to save text files containing any scratch work you may do while working on the pracs.
R Basics
At its most basic, R is a calculator. You can type basic mathematical functions into the R console (left pane) to
the right of the command prompt (‘>’) and run the code by pressing return on your keyboard.
1+1
## [1] 2
5*5
## [1] 25
1000^4
## [1] 1e+12
One of the reasons R is useful is the fact that you can store information by assigning it to objects. You can use
an arrow ( <- ) to assign a value to an object. Objects can be named anything, but cannot start with a number.
object1 <- 6 - 2
You can type the name of the object and run the code to see the contents of the object.
object1
## [1] 4
object2
If you look in the upper pane of your R session window, you should see objects 1 and 2 in your Environment
pane.
The other main utility of R is functions. A function takes basic inputs, runs some operations, and produces an
output. In R, functions are written as the name of the function followed by parentheses that contain the inputs
for the function.
One of the most commonly used and useful functions is the concatenate function: c() . This can be used to
create a vector. A vector is an object that has one or more numbers, words, or letters. Here we create a
numeric vector with three values.
object3
## [1] 6 8 12
R has many built in functions, like sum() , mean() , min() , and max() . There are others that can be added by
installing packages. Objects can be used as inputs for functions:
mean(object3)
## [1] 8.666667
To open a new script, click the icon on the upper left of your RStudio window and select “R Script”:
This will open a new (fourth) pane in the upper left of your R session above your console pane. You can type
code into the script pane. However, to run the code, you need to highlight it and press ctrl+return on your
keyboard (or command+return on a Mac), or hit the “run” button in the upper right corner above the script.
When you are saving your code in a script, it can be very helpful to add comments or reminders so that it is
clear what you did when you come back to your code or share your code with someone else. To add a
comment, insert # before the text.
vals <- c(6,3,8,13) #You can add comments on the same line as your code
output
## [1] 58.18182
To save your script when you have finished editing or when you want to take a break from R, select
File > Save as... and save the file into the Scripts folder.
3+12 ## addition
5 - pi ## subtraction
2*8 ## multiplication
14/5 ## division
10^2 ## power
exp(1) ## exponential
sin(pi/2) ## sine
cos(pi) ## cosine
tan(pi/4) ## tangent
factorial(10) ## factorial
To get information on a function in R you can just type in “?” before the function name (with no space between
them). So, if you don’t remember what a factorial is, you could type
?factorial
Within RStudio you can also search for help with commands in the Help tab in the bottom-right window. This
search window combines the functionality of both ? and help.search .
1. Create an object called vals1 and assign to it the values from 1 to 20.
Challenge 2
1. Create an object called vals2 and assign to it the values 5, 8, 12, and 1.
2. Create a new object vals3 that contains the product of vals1 and vals2 .
4. Look at the values contained in vals3 . How were the two objects, vals and vals2 multiplied
together?
Solutions
Challenge 1: 21
Challenge 2: min = 4, max = 228; first value of each object was multiplied together, then second, etc. At fifth
value in vals1 , we return to the first value of vals2 because it only has 4 values in it.