Assumptions & Ground Rules

(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.

Part 1 (Assignment 1.1)

Goal: Create new LastName_CRM495_work folder in your OneDrive cloud storage

(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)

  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).

  2. Create two new folders in your CRM495_work folder: a “Datasets” folder (LastName_CRM495_work > Datasets) and an “Assignments” folder (LastName_CRM495_work > Assignments).

    • You will use these folders in later assignments.
  3. 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).

  4. 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.

  5. 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).

Completing Assignment 1.1

Completing Assignment 1.1

Part 2 (Assignment 1.2)

Goal: Download R & RStudio; Configure and Familiarize yourself with RStudio.

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.

  1. 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.

  2. 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.

Changing RStudio Global Options

Changing RStudio Global Options

  1. 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.

      • Note: What are packages? To paraphrase Danielle Navarro: packages are self-contained “bundles of software” that extend the capabilities of R.
    • To install a package, simply go to the “Console” pane in RStudio and type: install.packages("tidyverse") and press enter.

      • Note: One thing to remember about packages, is you only have to install them once onto your computer, but in order to use them, you need to load them each R session. Because you only have to install them once, it’s generally good practice to install packages from the console pane and not an R Script.
  2. 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:

Installing Tidyverse

Installing Tidyverse

Part 3 (Assignment 1.3)

Goal: Open R Script, do some stuff.

  1. Open an “R Script” file (File > New File > R Script)
Opening a new R Script file

Opening a new R Script file

  1. Load the core “tidyverse” packages by typing, selecting, and running library(tidyverse) into your R Script file.

    • To run a line of code select the line of text (i.e. highlight with cursor) and click on “Run Selected Line(s)” in the dropdown “Run” menu in the upper right hand corner of RStudio’s script pane. Alternatively, you can use keyboard shortcuts to run a line (or lines) of code (Windows: CTRL + Enter; Mac: cmd + Enter)
Running a Line from an R Script file

Running a Line from an R Script file

  1. Create a plot by typing, selecting, and running the following code into your R Script file.
#Create plot using mtcars data
ggplot(
  data = mtcars,
  mapping = aes(x = mpg, y = disp)
) + 
  geom_point()
Creating Plot from an R Script file

Creating Plot from an R Script file

  • 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.

    • Note:The “mtcars” data set is built into base R. R comes with many different data sets preloaded and are often used as examples online when people are demonstrating various things you can do with R or asking questions about problems they are having with R. You’ll see the “mtcars” data used a lot when searching online for R help.
  • Your R script should now look similar to this:

    R Script with comments

    R Script with comments

    • Note: You’ll notice I added comments to the script by using a hashmark # 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.
  1. 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.

  2. 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

Part 4

Goal: Submitting your first assignment

  1. 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.

  2. 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.

    • The “commit” part indicates that you are ready to commit to sharing whatever you place in this folder. When collaborating with others, it is good practice to keep separate “work” and “commit” folders and then only to place a file or folder in your collaborative “commit” folder when is done and ready to be viewed (or, in this case, graded) by others.
  3. Inside the “LastName_CRM495_commit” folder in our shared folder, create another folder named: Assignment 1.

  4. 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.

    • Note: Be sure to save copies of both files - do not just drag the files over from your “work” folder, or you may lose those original copies from your “work” folder.
  5. 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.

    • Remember: you must make an earnest attempt and submit your assignment by 9:30am on the Thursday it is due. After we discuss and workshop issues with the assignment in class, you will have until 9:30am on Friday to make any revisions necessary and submit an updated file to Canvas and to your OneDrive folder.