Getting Started With R and RStudio
Getting Started With R and RStudio
1.1 Why R?
• R is not a programming language like C or Java.
• You can read the full history in the paper A Brief History of S.
result, there are numerous resources for learning and asking questions.
• It is easy for others to contribute add-ons which enables
methodologies.
• This gives R users early access to the latest methods and to tools
0.15 * 19.71
• It not only provides an editor for us to create and edit our scripts but also provides many other
useful tools.
• These are useful because color and indentation are automatically added to make code more
readable.
• One of the main advantages provided by RStudio over other editors is that we can test our code
easily as we edit our scripts.
• Let’s start by opening a new script as we did before.
• We can do this through the editor by saving the current new unnamed script.
• To do this, click on the save icon or use the key binding Ctrl+S on Windows and command+S on
the Mac.
• When you ask for the document to be saved for
the first time, RStudio will prompt you for a
name.
• This will help you avoid one of the most common errors in coding: forgetting to close a
parenthesis.
• As an example, we will make a graph showing murder totals versus population totals by state.
• Once you are done writing the code needed to make this plot, you can try it out by executing the
code.
• To do this, click on the Run button on the upper right side of the editing pane.
• You can also use the key binding: Ctrl+Shift+Enter on Windows or command+shift+return on the
Mac.
• Once you run the code, you will see it
appear in the R console and,
• This is to change the Save workspace to .RData on exit to Never and uncheck the Restore .RData into workspace
at start.
• By default, when you exit R saves all the objects you have created into a file called .RData.
• This is done so that when you restart the session in the same folder, it will load these objects.
• We find that this causes confusion especially when we share code with colleagues and assume they have this
.RData file.
• extra functionality from CRAN and many others shared via other repositories such as GitHub.
install.packages("dslabs")
• In RStudio, you can navigate to the Tools tab and select install packages. We can then load the
package into our R sessions using the library Function
library(dslabs)
• We can install more than one package at once by feeding a character vector to this function:
install.packages(c("tidyverse", "dslabs"))
• You can see all the packages you have installed using the following function:
installed.packages()
Installing R and RStudio
Installing R
• RStudio is an interactive desktop
environment, but it is not R, nor does
it include R when you download and
install it.
• You can now click through different choices to finish the installation. We recommend you select
all the default choices.
Installing RStudio
• Download Rstudio
• Once you select this option, it will take you to a page in which the operating system options are
provided. Click the link showing your operating system.
R - The very basics
Objects
• We will write out general code for the quadratic equation below, but if we are asked to solve x2+x−1=0, then we define
a <- 1
b <- 1
c <- -1
#> [1] 1
If you want a quick look at the arguments without opening the help system, you can type:
args(log)
#> function (x, base = exp(1))
#> NULL
You can change the default values by simply assigning another object:
log(8, base = 2)
#> [1] 3
Note that we have not been specifying the argument X
log(x = 8, base = 2)
#> [1] 3
OR
log(8,2)
#> [1] 3
OR
log(base = 2, x = 8)
#> [1] 3
Other prebuilt objects
• There are several datasets that are included for users to practice and test out functions. You can see all the available datasets by
typing:
data()
• This shows you the object name for these datasets. These datasets are objects that can be used by simply typing the name. For
example, if you type:
Co2
• R will show you Mauna Loa atmospheric CO2 concentration data.
• Other prebuilt objects are mathematical quantities, such as the constant π and ∞:
pi
#> [1] 3.14
Inf+1
#> [1] Inf
Variable names
• We have used the letters a, b, and c as variable names, but variable names can be
almost anything.
• Some basic rules in R are that variable names have to start with a letter, can’t
contain spaces, and should not be variables that are predefined in R. For example,
don’t name one of your variables install.packages by typing something
like install.packages <- 2.
• A nice convention to follow is to use meaningful words that describe what is
stored, use only lower case, and use underscores as a substitute for spaces. For the
quadratic equations, we could use something like this:
solution_1 <- (-b + sqrt(b^2 - 4*a*c)) / (2*a)
• Values remain in the workspace until you end your session or erase them with the function rm. But
workspaces also can be saved for later use. In fact, when you quit R, the program asks you if you want to
save your workspace. If you do save it, the next time you start R, the program will restore the workspace.
• We actually recommend against saving the workspace this way because, as you start working on different
projects, it will become harder to keep track of what is saved. Instead, we recommend you assign the
workspace a specific name. You can do this by using the function save or save.image. To load, use the
function load. When saving a workspace, we recommend the suffix rda or RData. In RStudio, you can also
do this by navigating to the Session tab and choosing Save Workspace as. You can later load it using the Load
Workspace options in the same tab. You can read the help pages on save, save.image, and load to learn
more.
Motivating scripts
Commenting your code
Example