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 “base R” and “RStudio” statistical programs and can open a SPSS datafile in RStudio.

For all assignments in this class, you must have access to a computer and should also use a cloud-based file system. (e.g. OneDrive, Dropbox). 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 cloud storage via your computer’s file explorer.

Also, 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_P680_work folder in your OneDrive cloud storage, then download datasets and save to your new folder (i.e., “work” indicates this is the cloud folder within which you will do most of your work for the course).

(Note: When following instructions, always substitute “LastName” for your own last name! Also, substitute YEAR_MO_DY for the actual date. E.g., Brauer_P680_Assign1_2021_08_23)

  1. On your computer, navigate to your OneDrive folder, then create a new folder called “LastName_P680_work” in a location that is easy to access (e.g., in the root OneDrive folder).
  2. Create two new folders in your P680_work folder: a “Datasets” folder (LastName_P680_work > Datasets) and an “Assignments” folder (LastName_P680_work > Datasets)
  3. Visit the Week 2 Module, below the “R Assignment 1: Introduction to R & RStudio” entry, on the class Canvas site to download the SPSS data file “MonitoringtheFuture2013grade10_0.sav.” This was downloaded from the companion website for Bachman & Paternoster’s Statistics for Criminology & Criminal Justice, 4th Ed.
  4. Save the dataset in the new “LastName_P680_work > Datasets” folder you made in Part 2.
  5. Open a Word document. At the top, put your name, date, course information (F21-P680; Dr. Brauer), and heading (Assignment 1). Then, create a subheading for Part 1 (e.g., Assignment 1.1).
  6. Take a screenshot (#1) of your new LastName_P680_work > Datasets folder with all six datasets downloaded in it and past into a 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.
  7. Save the Word document with your screenshot to your “LastName_P680_work > Assignments” folder. Name the file: LastName_P680_Assign1_YEAR_MO_DY (e.g., I would name my assignment: Brauer_P680_Assign1_2021_08_23).
    Completing Assignment 1.1

    Completing Assignment 1.1

Part 2 (Assignment 1.2)

Goal: Download R & RStudio; set Global Options; install “haven” and “here” packages.

In this section, you will begin by downloading and installing two programs on your computer: base 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. Follow the instructions at the link below to install the latest versions of R and RStudio on your personal Windows or Mac computer:
  2. Visit Antoine Soetewey’s blog (AS blog) entry at the link below, read the section titled “Main Components of RStudio,” and follow along in RStudio on your computer:
  3. 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”
    • 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. Because of this, unlike in the picture below, I also remove the check from “Restore most recently opened project at startup” and “Restore previously open source documents at startup.” While I recommend doing so as well, you can decide whether or not it is useful to you.
      Changing RStudio Global Options

      Changing RStudio Global Options

  4. Install “tidyverse” package
    • (Note: We will use various features of “tidyverse” in this class. For now, we are interested in the “haven” package installed with tidyverse. The haven package allows us to easily open the SPSS datafiles that you downloaded for Part 1 in RStudio.)
    1. Open an “R Script” file (File > New File > R Script) if you have not done so already (you should have if you followed along with the AS blog).
      Opening a new R Script file

      Opening a new R Script file

    2. Type install.packages("haven") into the R Script, select (i.e., highlight with cursor) this text line, and RUN the selection (Windows: CTRL + Enter; Mac: cmd + Enter)
      • The AS blog referenced above gives brief directions on installing packages and running R script selections. For more detailed instructions on installing packages, see Danielle Navarro’s video on the topic.
  5. Once installed, load the haven package by typing, selecting, and running the command library("haven") in your R Script file.
  6. Now, repeat the process above to install the “here” package.
    • Type install.packages("here") into your R Script file, select (highlight) this text line, and RUN the selection (Windows: CTRL + Enter; Mac: cmd + Enter)
  7. Once installed, load the here package by typing, selecting, and running library("here") into your R Script file.
    • The here package will help you start a replicable project-oriented workflow from the beginning. Here is how it will work once installed:
      • You save your primary R Script (or RMarkdown; we will learn about these later) code file in your top-level directory folder. For us, that means saving your R Script file in your LastName_P680_work folder.
      • Next, after closing RStudio, you will simply click directly on the R Script file in your LastName_P680_work folder to automatically open it with RStudio. When you do this, your “working directory,” i.e., the place R looks for files by default, will automatically be set to your P680_work folder.
      • The here package will then make it easy to find and call objects (e.g., SPSS dataset) in subfolders of your working directory (e.g., in “Datasets” folder).
  8. Check to see if the haven and here packages loaded properly.
    1. First, find the “Packages” tab (see “blue pane” in AS blog).
    2. Next, scroll down through the listed packages until you find the haven and here package entries.
    3. If there are checkmarks next to both entries, it worked!
      A checkmark means the R package is successfully loaded

      A checkmark means the R package is successfully loaded

  9. While you are at it, repeat the process above to install the tidyverse package.
  10. Take a screenshot (#2) of RStudio session, then paste it under a new Part 2 (Assignment 1.2) subheading in the “Assign1” Word document you created and saved earlier (in your “Assignments” folder).

Part 3 (Assignment 1.3)

Goal: Save R Script; use haven package to open SPSS file in RStudio

  1. In RStudio, click File > Save As, then save R Script file in LastName_P680_work folder. Name the file: LastName_P680_Assign1_RScript_YEAR_MO_DY
    Save R Script & Markdown files in top-level folder

    Save R Script & Markdown files in top-level folder

    • It is essential that your R Script files (and, later, R Markdown files) are saved in the top-level LastName_P680_work folder for the here package to work properly.
    • As in the picture above, your R Script should be in the same P680 folder as the “Datasets” and “Assignments” folders.
  2. After saving your R Script file, close RStudio, then reopen directly from R Script.
    • Open P680_work folder, locate new R Script file (LastName_P680_Assign1_RScript_YEAR_MO_DY), and click or double-click to open with RStudio.
    • By opening RStudio directly from the R Script file saved in your P680_work folder, the here package by default will set the P680_work folder as the top-level working directory
  3. Add #comments to R code and load haven and here packages
    1. In your R Script, place hashmarks (#) in front of both install.packages commands.
      • The haven and here packages should already be installed, so you should not need to run these lines again.
      • Placing a hashmark in front of a command – or in front of any text in R code – will create a “comment” that will not be run as a command in R.
    2. After commenting out the #install.packages commands, run both library commands to load the haven and here packages.
      • Though already installed, you will need to reload all packages that you want to use at the beginning of each R session.
      • Tip: Since you want to run all commands in your R Script that are not commented out (i.e., both library commands), try running the entire script instead of only a selection.
    3. Check the “Packages” tab to make sure there are checkmarks by the “haven” and “here” packages.
    4. Your R Script should now look similar to this:
      R Script with comments

      R Script with comments

    5. Note the red text in the Console that appeared after loading the here package. It confirms the here package has set my LastName_P680_work folder (i.e., for me, P680_work_Brauer) as the top-level working directory.
  4. Open SPSS dataset in RStudio using haven package and save data as an object in R
    1. Type the following command in your R Script file: read_spss(here(“Datasets”,’MonitoringtheFuture2013grade10_0.sav’))
      • read_spss() is a haven package command that allows R to read SPSS datafiles
      • Inside the read_spss() parentheses, we need to specify the file location and the filename we wish to open, separated by a comma.
      • Instead of specifying the exact path location, we specify here(“Datasets”, which uses the here package to locate our “Datasets” folder. By using the here package, your R code should work for anyone on any computer as long as they have the exact same file structure (e.g., you can work from local or cloud storage, or work from different computers)
      • Next, our code specifies the ’MonitoringtheFuture2013grade10_0.sav’ file in the “Datasets” folder to open.
    2. Run the read_spss command. You should see a small snapshot of the data in your R console, which looks something like this:
      read_spss command shows snapshot of data in R Console

      read_spss command shows snapshot of data in R Console

    3. You just read a dataset into RStudio!
    4. However, you do not want the dataset in the R Console – you want to be able to call the dataset as an object on which you can do things, such as summarize or correlate variables (later). To do this, you need to save the dataset into the R Environment. (Note that your R Environment tab is still empty.)
    5. To save the dataset as an object in R, type MF2013g10data <- before the read_spss() command.
      • The text MF2013g10data is the name we are giving to the data object we are creating.
      • The <- command tells R to place whatever follows the text arrow (in this case, the dataset read by the read_spss command) into our MF2013g10data object.
        • ****Note:*** We can name the object whatever we want (e.g., mydata). However, it is good coding practice to be systematic in creating concise yet informative names. The name I selected reminds me that the object is the Monitoring the Future 2013 grade 10 data.
        • Also, R code is case-specific, so be careful and consistent with using upper- and lower-case letters!
        • By this point, you may be wondering why I care so much about how you name your folders and files. The short answer is that thoughtful and systematic naming conventions can save you a lot of time and can be very helpful to your future self or to others attempting to reproduce your work. In contrast, ad hoc names can cause a great deal of unnecessary frustration. I recommend Danielle Navarro’s videos on Names machies like and Names humans like for more information and useful file naming tips.
    6. Your R Script code should look like this:
      Example R Script for reading SPSS Data into R Object

      Example R Script for reading SPSS Data into R Object

    7. Run the line of code in your R Script to create the new data object.
  5. Take a screenshot (#3) of RStudio session. Paste into your “Assign1” Word document (saved in “Assignments” folder) under a new Part 3 (Assignment 1.3) subheading.

Part 4

Goal: Submitting your first assignment

  1. After completing the first four parts of this assignment, you should have one “Assign1” Word document in your “Assignments” folder that contains all three (3) of your screenshots. Additionally, you should have an “Assign1_RScript” file in your LastName_P680_work folder.
  2. Previously, I sent you an invite to a shared OneDrive folder (“Brauer-F21-P680”). Navigate to our shared folder. Inside this shared folder, create a new folder named: LastName_P680_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_P680_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) “Assign1” Word file and (2) your “Assign1_RScript file” into the LastName_P680_commit > Assignment 1 folder. 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.