Tutorial 1: Installation and RStudio Basics
In this course, we will use the R statistical programming language within the development
environment called RStudio, both of which are downloadable from the following links for free.
Step 1: Download R (https://cloud.r-project.org/) and install on your machine
Select one
Step 2: Download RStudio (https://www.rstudio.com/products/rstudio/download/#download)
and install on your machine
and
Select one
Step 3: Launch RStudio
After you download and install the appropriate versions of R and RStudio, launch RStudio. Your
screen should look like this:
Step 4: Click the button at the top left (circled area) in the above screenshot and select R Script.
This opens the script window in the Rstudio development environment. Your RStudio screen
should now look like this:
Please note that the RStudio window is divided into four segments. Let us discuss each segment.
Top Left
This section is where you will write your R code in a script that you can run. You can save this
script in a file to look at and edit later if need be. We will revisit this later (Creating R Scripts) as
most of your work will be in this section.
Top Right
The environment window is where your data and objects are stored. If you create a variable, it
will be shown here.
At the end of this tutorial, your environment will contain a few variables and objects, like the
environment shown below.
Bottom right
The Files tab in this section allows you to browse the files in your computer, starting where the R
Script file you created is. You can change the save location of your script file by clicking File
and then Save As.
The bottom right section also contains a Plots tab that shows the graphs you have created. In
addition, it includes a Packages tab, a Help tab, and a Viewer tab.
Here is what a sample graph (in the Plots tab) looks like:
Bottom left
The console runs any typed R code directly. Type the following on the console and see what
happens.
assigns 5 to variable x
displays value of x
result
As you can see, the console runs the code one line at a time. The way the result is displayed
needs an explanation.
R works with data structures, the simplest of which is a vector with only one
element. Therefore, in the output [1] indicates the index of the element, whereas 5
indicates its value.
Please note that the environment tab (the top right portion of the RStudio window) now includes
x in the list of variables.
Creating R Scripts
Type the following code in the script window (the top left section) and click on Run (circled area
below).
The environment window now includes variable y. Note that the Run button runs only the line
the cursor is positioned on (line 2). Consequently, when the code on line 2 was executed, the
current value of x (5) was incremented rather than the value assigned on line 1 (2). In other
words, the assignment statement on line 1 was skipped over.
Now press Ctrl + Alt + R to execute the entire script on
the script window. As expected, the environment
window shows the updated variable values. You can
see that value of x is now 2 instead of 5 and the value of
y changed to x + 1.
Data Frames
One of most commonly used data structures in R is the data frame. Data frames are basically
matrices with rows and columns, where each column can possibly store a different type of data
(character, number, etc.). The following script presents a simple demonstration of data frames.
Before we study this simple example, let us note that any line that starts with a # is designated as
a comment line. This means that whatever follows # will simply be ignored. Rstudio uses color
coding to differentiate different type of constructs. For example, comments appear in green.
The c() function is used to group a set of data points. In the example above, 10 integer data
points are grouped into one list named Vector_1, which is then placed in a data frame named
My_DataFrame. Please remember that a data frame can include multiple groups of data.
The next line in the script executes the print command to display the contents of this data frame.
The console window shows the result.
The environment window now includes the data frame object as well. The data frame entry can
be expanded by clicking on the arrow (in the circled area above) to reveal the individual values
in the data frame.
Online R tutorials:
(1) http://cyclismo.org/tutorial/R/
(2) https://www.tutorialspoint.com/r/index.htm
(3) https://cran.r-project.org/doc/manuals/r-release/R-intro.html
R Packages: A Beginner’s Guide