Introduction to R and RStudio Answer Key

Author

Will Gammerdinger

Published

May 30, 2025

Exercise 1

To organize your working directory for a particular analysis, you should separate the original data (raw data) from intermediate datasets. For instance, you may want to create a data/ directory within your working directory that stores the raw data, a scripts/ directory for your R scripts, a results/ directory for intermediate datasets and a figures/ directory for the plots you will generate.

We have provided you with R project containing the data/ directory and we made our scripts/ directory together. For this exercise create the results/ and figures/ directories.

When finished, your working directory should look like this:

Click on the New Folder button of the “Files” tab. Type results and click “OK”. Click on the New Folder button of the “Files” tab. Type figures and click “OK”.

Exercise 2

Try highlighting only 3 + from your script editor and running it. Find a way to bring back the command prompt > in the console.

Press the ESC button.

Exercise 3

Try changing the value of the variable x to 5. What happens to number?

# Show value of number
number
[1] 8
# Change the value of x to 5
x <- 5
# Show value of number
number
[1] 8

It doesn’t change.

Now try changing the value of variable y to contain the value 10. What do you need to do, to update the variable number?

# Show value of number
number
[1] 8
# Change the value of y to 10
y <- 10
# Update the value of number
number <- x + y
# Show value of number
number
[1] 15