(Note: This document was created as an R Markdown file. You will learn about R Markdown files later. First, I want to familiarize you with the RStudio interface.)
The purpose of this first assignment is to demonstrate that you have downloaded the “R” statistical program and the “RStudio” Integrated Development Environment (IDE).
For all assignments in this class, you must have access to a computer and should also use Microsoft OneDrive at UNCW, the recommended UNCW cloud-based file systems. Working from cloud storage is essential - and not just for this class! By saving and backing up using cloud storage, you will be able to access your saved files from any computer, you will still have access to your saved files if your computer were to unfortunately stop working, and you can easily share files and folders with collaborators.
Thus, for this and all future assignments, I will assume you are working on your own computer, are downloading base R and RStudio (free open source) programs on your own computer, and are working from your Microsoft OneDrive cloud storage via your computer’s [Windows] file explorer (Windows or MAC). If you experience technical difficulties, remember that you can contact TAC for help.
If you do not have access to your own computer or cannot download R/RStudio, you should be able to access the software on any computer on or off UNCW’s campus via Horizon.
Whether you download R or access it elsewhere (e.g., via Horizon), the remainder of the steps must be completed on a computer for grading purposes.
Important: For this and all future assignments, you MUST type all commands in by hand. Do not copy & paste except for troubleshooting purposes (i.e., if you cannot figure out what you mistyped). Trust me - you will learn better from typing.
Early on, you may have a lot of trouble getting your code to run due to minor typos. This is normal. Remember, you are learning to read and write a new (coding) language. As with learning any new languages, we learn from practice - and from correcting our mistakes.
(Note: When following instructions, always substitute “LastName” for your own last name! Also, substitute YEAR_MO_DY for the actual date. E.g., **Day_CRM495_RAssign1_2022_01_1)
On your computer, navigate to your OneDrive folder, then create a new folder called “LastName_CRM495_work” in a location that is easy to access (e.g., in the root OneDrive folder).
Create two new folders in your CRM495_work folder: a “Datasets” folder (LastName_CRM495_work > Datasets) and an “Assignments” folder (LastName_CRM495_work > Assignments).
Open a Word document. At the top, put your name, date, course information (SP22-CRM495; Day), and heading (Assignment 1). Then, create a subheading for Part 1 (e.g., Assignment 1.1).
Take a screenshot (#1) of your new LastName__work folder with the “Datasets” and “Assignment” folders and paste into the word document.
PC: ctrl + prt sc then ctrl + P into a Word document underneath the subheading for Part 1. Or, in the search bar type in “snipping tool” and use the tool to take a snapshot of your Datasets folder.
Mac: Command + shift + 3, this saves on your desktop. Then, paste into the Word document under the Part 1 subheading.
Save the Word document with your screenshot to your “LastName_CRM495_work > Assignments” folder. Name the file: LastName_CRM495_RAssign1_YEAR_MO_DY (e.g., I would name my assignment: Day_CRM495_RAssign1_2022_01_20).
In this section, you will begin by downloading and installing two programs on your computer: R and RStudio.The first program, R, is simultaneously a computer coding language and a statistical software program. The second, RStudio, is an integrated development environment (IDE) that provides a more user-friendly interface for working with the R program. Throughout this course, you will learn to write and submit R code in RStudio to run statistical commands in the R program. After installing R & RStudio, you will run some simple commands to familiarize yourself with the basic features of the program and install two R packages.
I recommend following along with Danielle Navarro’s videos for for installing R and R Studio in Windows or on a Mac.
Alternatively, you could also follow the instructions here.
Antoine Soetewe’s blog entry, particularly the section titled “Main Components of RStudio” will also guide you through the process.
At the top of RStudio, navigate to “Tools” then click “Global Options” and change the following settings:
Uncheck “Restore .RData into workspace at startup”
Change “Save workspace to .RData on exit” to “Never”
Uncheck both boxes under “History”
Uncheck “Restore most recently opened project at startup”
Uncheck “Restore previously open source documents at startup.”
Our goal is to write and save standalone reproducible code that can be run at any time by anyone with the file and associated file structure. In my experience, these options aid in pursuit of that goal while minimizing headaches when collaborating with others using shared drives. For additional information, see Section 4.5 in Nicholas Tierney’s R Markdown for Scientists.
Install “Tidyverse” suite of packages.
The “Tidyverse” suite of packages is a popular set of packages for teaching and learning data science that extend the functionality of R. We will use various features of and specific packages within the “tidyverse” in this class.
To install a package, simply go to the “Console” pane in RStudio and type: install.packages("tidyverse")
and press enter.
Take a screenshot (#2) of your RStudio session (make sure the console window where you installed the “tidyverse” package is visible), then paste it under a new Part 2 (Assignment 1.2) subheading in the “RAssign1” Word document you created and saved earlier (in your “Assignments” folder). It should look something like this:
Load the core “tidyverse” packages by typing, selecting, and running library(tidyverse)
into your R Script file.
#Create plot using mtcars data
ggplot(
data = mtcars,
mapping = aes(x = mpg, y = disp)
) +
geom_point()
Here we use the function ggplot
from the “ggplot2” package that is part of the core set of “tidyverse” packages and was automatically loaded when you typed library(tidyverse)
earlier.
Your R script should now look similar to this:
#
before some brief, informative, statement about what is happening in the code (e.g. “#Load tidyverse package”). Placing a hashmark in front of a command – or in front of any text in R code – creates a “comment” that will not be run as a command in R. It is one way to make sure your future self and others know what is going on in your script.Take a screenshot (#3) of RStudio session. Paste it into your “RAssign1” Word document (saved in “Assignments” folder) under a new Part 3 (Assignment 1.3) subheading.
In RStudio, click File > Save As, then save R Script file in LastName_CRM495_work folder. Name the file: LastName_CRM495_RAssign1_RScript_YEAR_MO_DY
After completing the first three parts of this assignment, you should have one “RAssign1” Word document in your “Assignments” folder that contains all three (3) of your screenshots. Additionally, you should have an “RAssign1_RScript” file in your LastName_CRM495_work folder.
Previously, I sent you an invite to a shared OneDrive folder (“Day-SP22-CRM495”). Navigate to our shared folder. Inside this shared folder, create a new folder named: LastName_CRM495_commit.
Inside the “LastName_CRM495_commit” folder in our shared folder, create another folder named: Assignment 1.
To submit your assignment for grading, save copies of both your (1) “RAssign1” Word file and (2) your “RAssign1_RScript file” into the LastName_CRM495_commit > Assignment 1 folder.
Finally, submit your word document on Canvas in the “R Assignment 1” submission portal. This will allow me to have a time-stamped version of your assignment for grading purposes.