Appendix A — Installing R and RStudio

Author

Dr. Alejandro Molina-Moctezuma

A.1 Install R and RStudio

A.1.1 Install R

R can be downloaded from the following page https://cran.r-project.org/, follow the instructions to download it.

A.1.1.1 Windows

To install R on Windows, click the “Download R for Windows” link. Then click the “base” link. Next, click the first link at the top of the new page.

A.1.1.2 Mac

To install R in a Mac the “Download R for Mac” link. Next, click on the R-4.4.2 package link.

A.1.1.3 Chromebook

Chromebooks don’t allow you to install R or the RStudio program. However, there is an online platform that will allow you to run RStudio. If you go to https://posit.cloud , you should be able to create an account, and in your workspace you can open a new RStudio project. Your window will look the same as what we are working with, though there may be some quirks to getting

A.1.2 Install RStudio

Go to https://posit.co/download/rstudio-desktop/

Click on the Download RStudio Desktop link. If you are using a Mac, you will need to scroll down a bit to see the download link.

A.2 Explore R and RStudio

RStudio is a program, not different than Word. It works as a “wrapper” or “editor”. You will write the code in RStudio, and it will run it in program R. You will rarely or maybe never open program R. You will do everything from RStudio!

Tip

RStudio needs R to function, so you need to download both programs!

After you open RStudio, this is what you will see:

RStudio Window

Now, you can code in R!

A.2.1 Let’s check R and RStudio!

Let’s do a couple of things in R. Run the following lines (or similar, you can use different numbers!) on the Console (big screen on the left!)

First, let’s do basic math:

5+5
10*7
89-5
90/3

As you can see, we can use R as a calculator

Now, let’s create a vector

1:10
120:170
Try it! ✏️

Now, try to create a vector from 200 to 300

Finally, let’s roll a die!

We will roll a “D20” die (this is a die with 20 sides). First, we need to create this die. We can create objects in r using the following symbol: <- . Objects will save data, and we can use the object name to extract the data. We can create our dice using the following:

\[ \underbrace{D20}_{Object\; name} \; \; \; \underbrace{<-}_{arrow} \; \; \underbrace{1:20}_{Data} \]

The arrow is created with these two symbols: < and - .

D20<-1:20

Now, if you do the following:

D20

You extract the data from the object. Pretty cool!

Now, let’s roll our die!

sample(x=D20, size=1)

Let’s now roll 3 D20 dice:

sample(x=D20, size=3, replace=TRUE)
Think about it! 🧠

What do you think the replace=TRUE does?