Chapter 1: Introduction Appendix Getting Started with R and RStudio
Book Title: Business Analytics
Printed By: Kathleen Lloyd (kathleen.lloyd@eastern.edu)
© 2021 Cengage Learning, Cengage Learning
Chapter Review
Appendix Getting Started with R and RStudio
In this appendix, we describe how to get started with R and RStudio. R is an open-source
statistical software package widely used for data analysis and data mining. RStudio is an
opensource integrated development environment (IDE) that provides a graphical user
interface to facilitate the use of R. The following steps outline how to install R and RStudio.
Step 1.
Open an Internet browser and go to https://cran.r-project.org/. Download and
install the version of R corresponding to the appropriate operating system
(Windows, Mac, or Linux)
Step 2.
Once R is installed, proceed to www.rstudio.com to select and install the
version of RStudio corresponding to the appropriate operating system
(Windows, Mac, Ubuntu Linux, or Fedora Linux)
Once R and RStudio have been installed, we can begin familiarizing ourselves with this
platform. As we will soon learn, R script files are useful ways to save a series of R
commands for executing data analysis that we would like to reproduce. The following steps
open RStudio and a new file in the Script Editor.
Step 1.
Click on the
icon to open the RStudio window and begin working with R
Step 2.
To open a new R script file in the upper-left corner of the RStudio window,
from the RStudio menu select File, then New File, and then R Script
As Figure R1 displays, the RStudio
window is organized into panes.
The Script Editor in the upper lefthand corner of the RStudio
window contains a new (untitled)
file.
The Console pane in the lowerleft corner is where the user can
The Terminal, Connections, and Viewer
panes are utilized in more advanced uses of
R. The Terminal pane provides access to the
system shell for access to the user’s
operating system’s services. The
Connections pane enables the direct
connection to databases in R. The Viewer
pane displays local web content.
enter R commands and view
output.
The Environment pane in the upper-right corner lists all active data objects (vectors,
matrices, lists, functions, data frames, etc.) created in the R session.
The History pane is also in the upper-right corner and can be accessed by clicking on
the History tab. The History pane displays a list of commands used during the
session. The History pane can be useful for selecting commands to be saved in an R
script to record and track your work.
The Files pane is in the lower-right corner, and it shows the folders and files in the
current directory.
The Plots pane is in the lower-right corner and can be accessed by clicking on the
Plots tab. It contains the graphs generated by the R commands. If multiple plots are
generated, it is possible to toggle through them using the arrow icons
on the
Plots ribbon. Plots can be exported using the Export icon on the Plots ribbon.
The Packages pane is in the lower-right corner and can be accessed by clicking on
the Packages tab. It lists the packages available to extend the base R functionality. If
the box next to a package is checked, the package is loaded into R and its
functionality can be used. Additional packages not listed here can be added by
clicking Install in the Packages pane. Similarly, updates to packages can be obtained
by clicking Update.
The Help pane is also in the lower-right corner and can be accessed by clicking on the
Help tab. It provides searchable help pages.
Figure R1
RStudio Window
The working directory is the folder on your computer where R is currently operating. When
accessing files and saving files, awareness of the working directory will often prevent input–
output errors. The following steps allow the user to change the working directory.
Step 1.
From the RStudio ribbon, select Session, then Set Working Directory, and
then Choose Directory…
Step 2.
When the Choose Working Directory window opens, navigate to the desired
folder to specify the working directory
Typically, the Console pane is where a
user will experiment with various R
commands. After identifying the R code
that executes the desired analysis, it is
good practice to copy and paste it into a
script file (in the Script Editor area in
upper-left region of the RStudio
window). R code entered into the script
editor can then be saved using the save
button
and the analysis reexecuted
without having to retype all the commands.
To download a PDF of tips and shortcuts for
using RStudio, click Help -> Cheatsheets
-> RStudio IDE Cheat Sheet.
Cheatsheets are also available for commonly
used R packages such as dplyr, ggplot2, and
shiny.
To build familiarity with R syntax and functionality, let us now experiment with it using some
simple commands. As illustrated in Figure R2, we enter a list of commands into the Script
Editor. To execute the code in an R script, place the cursor on the line of code to be
executed and click the
icon in the Script Editor toolbar. Alternatively, multiple lines
of code can be executed at once by highlighting all the lines desired and then clicking the
icon in the Script Editor toolbar (or by pressing Control+Enter). We save this list
of commands as BasicCalcs.R.
The R assignment operator <- is used to assign values to variables. The lines in Figure R2
do the following:
Line 1: The variable z is assigned a value of 3.
Line 2: The variable y is assigned a value of −2.
Line 3: x is assigned to the vector (1, 2, 6) using R’s combine function c, which combines
all supplied arguments into a vector.
Line 4: w is assigned to the vector of integers between −1 and 4, (−1, 0, 1, 2, 3, 4), using
the : operator.
Line 5: v is assigned to z*y = 3*(−2) = ‒6.
Line 6: u is assigned to z/y = 3/(−2) = ‒1.5.
Line 7: t is assigned to x+y = 3+(−2) = 1.
Line 8: s is assigned to x+y = 3−(−2) = 5.
Line 9: r is assigned to the vector x*z = (1, 2, 6)*3 = (1, 2, 6)*(3,3,3) = (1*3, 2*3, 6*3) = (3,
6, 18).
Line 10: q is assigned to the vector x+w = (1, 2, 6) + (−1, 0, 1, 2, 3, 4) = (1, 2, 6, 1, 2, 6) +
(−1, 0, 1, 2, 3, 4) = (1+(−1), 2+0, 6+1, 1+2, 2+3, 6+4) = (0, 2, 7, 3, 5, 10).
Line 11: p is assigned to the vector x*w = (1, 2, 6) * (‒1, 0, 1, 2, 3, 4) = (1, 2, 6, 1, 2, 6) *
(−1, 0, 1, 2, 3, 4) = (1*(−1), 2*0, 6*1, 1*2, 2*3, 6*4) = (−1, 0, 6, 2, 6, 24).
Line 12: o is assigned to the vector x/w = (1, 2, 6) / (−1, 0, 1, 2, 3, 4) = (1, 2, 6, 1, 2, 6) /
(−1, 0, 1, 2, 3, 4) = (1/(−1), 2/0, 6/1, 1/2, 2/3, 6/4) = (−1, Inf, 6, 0.5, 0.667, 1.5).
Line 13: n is assigned to the vector resulting from the sqrt function being applied to w
.
Figure R2
Basic R Calculations
R applies operations element-wise to vectors. In Lines 9–13, we observe that when applying
an operation to two vectors of unequal length, the elements of the shorter vector are
repeated to match the length of the longer vector. In Line 12, we see that the output of the
second element’s operation of 2/0 is represented by Inf which represents infinity. In Line 13,
we see that the output of the first element’s operation of
is represented by NaN which
stands for “not a number.”
If we highlight all lines using the mouse and click the
icon, the results as they
appear in the Environment and Console panes are as shown in Figures R3 and R4. Note
that the results shown in Figure R3 match those previously shown. In Figure R4 we see that
the commands executed are displayed, along with a warning message that a NaN was
produced.
Figure R3
The RStudio Environment Pane After Running the Commands in
BasicCalcs.R
Figure R4
The RStudio Console Pane After Running the Commands in BasicCalcs.R
Although R can be used for calculations and other more advanced coding, the real power
for R is in the use of the many statistical packages that have been developed to perform
elementary and advanced statistical analyses. To use these powerful packages, you need to
know how to load packages to your copy of R.
The following steps describe how to install and load a package that is not listed in the
Packages pane. Note that you must have an active Internet connection to install new R
packages.
Step 1.
Select the Packages pane and click
Step 2.
When the Install Packages window opens (Figure R5), enter the name of the
R package—in this case, dplyr and click Install. After the package is
installed, it will appear in the list of packages in the Packages pane. Be
patient, package installation can take several minutes
Step 3.
In the Packages pane, select the check box next to dplyr
Figure R5
R Install Packages Window
Information on how to use a package
can be obtained by using the vignette
command. For example, to display a
description of the dplyr package in the
Help pane, enter the following in the
Console pane:
Pressing Ctrl+L visually clears the
Console (but not the memory). The function
rm(list = ls()) clears the Environment
memory of all data objects. An individual
data object can be cleared as well by using
the rm function with the name of the data
object to be cleared as the argument.
R packages exist for virtually every statistical method in this text. Three of them are:
dplyr, which provides a set of commands to more easily manipulate data and
databases;
ggplot2, which provides a set of commands for data visualization; and
forecast, which provides a set of commands to generate forecasts using time-series
methods.
It is a good practice to regularly update your R packages to ensure compatibility and
correctness. The following steps describe how to update packages in RStudio. Note that
you must have an active Internet connection to update R packages.
Step 1.
From the RStudio menu, select Tools and then Check for Package
Updates…
Step 2.
When the Update Packages window opens (Figure R6), select Select All and
then click Install Updates
The exact R packages displayed here will
depend on the packages that require
updates in your version of R at the time you
select Check for Package Updates…
Figure R6
R Update Packages Window
Notes + Comments
For many of the step-by-step R directions in our appendixes, we provide the R script
files as MODELfiles that contain all the commands included in the step-by-step
instructions. We encourage students to practice entering the R commands into
RStudio, but the R scripts can be helpful to find errors caused by entering R
commands by hand that are causing errors.
Chapter 1: Introduction Appendix Getting Started with R and RStudio
Book Title: Business Analytics
Printed By: Kathleen Lloyd (kathleen.lloyd@eastern.edu)
© 2021 Cengage Learning, Cengage Learning
© 2025 Cengage Learning Inc. All rights reserved. No part of this work may by reproduced or used in any form or by any means - graphic, electronic, or mechanical, or in any other manner - without the written permission of the copyright holder.