Assignment 1 Objectives

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.

This document was created as an R Markdown file. You will learn about R Markdown files later in the assignment. First, I want to familiarize you with the RStudio interface.

By the end of assignment #1, you should…

  • have created folders on both your personal computer and in cloud storage
  • have 6 full datasets downloaded
  • know how to create R Markdown (RMD) document in RStudio
  • know how to add and modify text, including italic or bold font and level headings, in an R Markdown document
  • know how to add and use an R code chunk in an RMD file
  • know how to use install.packages() and library() commands to install and load packages in R
  • know how to add hashtags (“#”) to comment out a section of R code so it does not run
  • be able to use the “here” package and here() function for simple, reproducible file directory referencing
  • be able to read data into R/RStudio using read_spss() function from “haven” package, then assign it to an object in the R environment using an assignment (<-) operator
  • be able to knit your RMD file into a Word document that you can save and submit for course credit

Assumptions & Ground Rules

For all assignments in this class, you must have access to a computer and Microsoft Word. As a TAMUSA student you have access to the Microsoft 365 suite, which includes 1TB of storage on OneDrive. You may find instructions for how to access and install programs (such as Microsoft Word) from Microsoft 365 in the Resources & Supplemental Texts folder under the Course Content tab in Blackboard.

For this and all future assignments, I will assume you are working on your own computer and are downloading base R and RStudio (free open source) programs on your own computer. If you experience technical difficulties, remember that you can call ITS at 210-784-4357 or email them at for help.

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

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 CRIM5305_L_LastName folder on your local computer, then download datasets and save to your new folder (i.e., “L” for “local” computer folder).

(Note: When following instructions, always substitute “LastName” for your own last name! Also, substitute YEAR-MO-DY for the actual date. E.g., 2023-01-27_Ducate_CRIM5305_Assign01)

  1. On your computer, create a new folder called CRIM5305_L_LastName in a location that is easy to access (e.g., on Desktop or in “My Documents”).

  2. Create a new folder in your CRIM5305_L folder called Datasets (CRIM5305_L_LastName > Datasets)
  3. Visit the Companion Website here for Bachman, Paternoster, and Wilson’s Statistics for Criminology & Criminal Justice, 5th Ed (specifically, under the heading: R Data Sets, Full Versions) for the datasets.
    1. Under the header Data Sets for SPSS Full Version, you will see six datasets:
      • Monitoring the Future 2013 grade 10_0.sav
      • NCVS lone offender assaults 1992 to 2013.sav
      • Youth_0.sav
      • 2012 States Data.sav
      • 2013 YRBS.sav
      • GSS 2014.sav
  4. Download each dataset and save them all in the new “CRIM5305_L_LastName > Datasets” folder you made in Part 2.
    • NOTE: To download, you may need to right click and select “Save link as…”

    • Do NOT try to open the datasets after you have downloaded them. If your computer asks which program to open them with, press CANCEL. Do not open them in Microsoft Word. Simply download them. We will open them in RStudio in Part 4.

  1. Take a screenshot (#1) of your new CRIM5305_L_LastName > Datasets folder with all six datasets downloaded in it.
    • PC (Option 1): ctrl + prt sc then ctrl + P into a Word document.
    • PC (Option 2): In the search bar type in “snipping tool” and use the tool to take a snapshot of your Datasets folder.
    • Mac (Option 1): Command + shift + 3; this takes a screenshot of your entire screen and saves on your desktop.
    • Mac (Option 2): Command + shift + 4; this allows you to click and drag to select which part of your desktop you want to screenshot; this also saves on your desktop.

  2. Insert the screenshot into a Word document to your CRIM5305_L_LastName folder. Name the file: YEAR-MO-DY_LastName_CRIM5305_Assign01_folder-screenshot
  3. (Optional) Create a folder titled CRIM5305_C_LastName in your cloud storage (e.g., iCloud, Dropbox, OneDrive).
    • If you have your cloud storage synced to your computer, you can work out of this folder instead of your CRIM5305_L_LastName folder.

    • If you choose to work out of this folder instead of your local folder (which I recommend), be sure to copy your Datasets folder into this folder as well.


Part 2 (Assignment 1.2)

Goal: Download R & R Studio; Open R Markdown; Knit first RMD file

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: https://www.datacamp.com/community/tutorials/installing-R-windows-mac-ubuntu. For more detailed instructions, check out Danielle Navarro’s videos for installing R and R Studio in Windows or on a Mac.

  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: https://towardsdatascience.com/how-to-install-r-and-rstudio-584eeefb1a41

  3. Once you have installed RStudio, open it.
  4. In RStudio, open an “R Markdown” file (File > New File > R Markdown…).
    1. Note: An R Script, which is the default file in RStudio, allows us to write and run code within R. However, an R Markdown file does this as well, while also permitting us to do so much more. For instance, you can write and edit text, write and run R code, and generate statistical results and plots directly in the RMarkdown file. You can even create entire books and webpages using R Markdown. In fact, this assignment was created using R Markdown!
    2. R Markdown is an essential tool for producing reproducible research because, with it, we can thoroughly document and simultaneously provide detailed explanations for all of our coding decisions in a project - from opening and manipulating data, to recoding and combining variables, to summarizing and analyzing data, to creating and modifying figures.
    3. We will start by simply opening and saving a new R Markdown file. For more detailed instructions, check out Danielle Navarro’s video on creating a new R Markdown file.
Opening a new R Markdown file

Opening a new R Markdown file

  1. The dialogue box asks for a Title, an Author, and a Default Output Format for your new R Markdown file.
    1. In the Title box, enter CRIM5305 Assignment 1.
    2. In the Author box, enter your First and Last Name (e.g., Caitlin Ducate).
    3. Under Default Output Format box, select “Word document” (HTML is usually the default selection)
      • (Note: You must have Microsoft Word installed for this to work properly. TAMUSA students can install Microsoft word [and the whole Microsoft 365 suite] for free.)
    4. Click OK to create your new R Markdown file. It should look like this:
Your new R Markdown file

Your new R Markdown file

  1. The new R Markdown file contains a simple pre-populated template to show users how to do basic tasks like add settings, create text headings and text, insert R code chunks, and create plots. Feel free to read through the template - you may find it helpful. Personally, I find the template a little distracting and a bit overwhelming for new users. So, we are going to delete everything after the metadata and second set of three dashes (i.e., after the YAML header). Your R Markdown file should look like this:
Keep the YAML header; delete the template

Keep the YAML header; delete the template

  1. Familiarize yourself with R Markdown by adding some headers, text, and R code chunk.
    1. Hit <Enter> on your keyboard to leave a blank line between the header and the first line of text.
    2. On the next line (line 8), type: ## Part 1 (Assignment 1.1)
      • In the markdown document, two hashmarks specifies a second-level text heading. Be sure to include a space after the last hashmark.
      • Note: This is different from a R Script file, which only contains R code. A hashmark transforms code into a comment that is not evaluated or run in an R Script file (and in an R code chunk in R Markdown, which you will learn about below).
    3. Hit <Enter> and, on the next line (line 9), type: ### Learning R Markdown
      • Three hashmarks specifies a third-level text heading.
    4. Hit <Enter> to leave another blank line (line 10)
    5. On the next line (line 11), type the following sentence: This R Markdown document contains my work for Assignment 2. It is my work and only my work.
      • To italicize, place a single asterisk (*) before and after the word or text segment.
      • To bold, place two asterisks (**) before and after the word or text segment.
      • To bold and italicize, place three asterisks (***) before and after the word or text segment.
      • There are a lot of sources online that explain various formatting options in R Markdown. For examples, check out here, here, here, here, and here. Also, check out Nicholas Tierney’s bookdown for descri ptions of and solutions to common problems with RMarkdown.

  2. Before typing anything else, save your new R Markdown file as: YEAR-MO-DY_LastName_CRIM5305_Assign01. Your RStudio session should now look similar to this:
Your first R Markdown file

Your first R Markdown file

  1. Ready for one of the best parts of R Markdown? You can use the “Knit” button at the top of your R Markdown file to automatically create a Word document capturing your current work.
    1. Click the knit button
      • Note: You can “Knit” your R Markdown document anytime. This can be helpful when you are getting used to working with R Markdown, as it allows you to continuously review the current state of your text and code. Just be sure to comment out any code chunks that are unfinished or incorrect, otherwise the document will not “Knit”. You will learn about commenting later in the assignment.
    2. A Word document should pop up that looks a lot like this:
Your first knitted html document using R Markdown

Your first knitted html document using R Markdown

  1. Now, in R Markdown, you are ready to insert a code chunk and begin reading in and assigning your data to an object in R!

Part 3 (Assignment 1.3)

Goal: Insert R code chunk in R Markdown; Install haven (with tidyverse) and here packages then load in R

  1. Create a second-level header in R Markdown (hereafter, “RMD”) file titled: “Load Libraries”

  2. Insert an R code chunk

  1. Click “Code > Insert chunk” or click the “Insert code chunk” button (green box with a “C” in it) and select “R” option (see below).
  2. Type install.packages("tidyverse") into the code chunk and hit the right-pointing green arrow on the right side of the chunk.
    • You can also highlight the text with your cursor, hit RUN, or hit (Windows: CTRL + Enter; Mac: cmd + Enter). The AS blog referenced above gives brief directions on installing packages and operating RMD. For more detailed instructions on installing packages, see Danielle Navarro’s video on the topic.
Insert R code chunk

Insert R code chunk

Insert R code chunk

Insert R code chunk

  1. Once installed, load the haven package by typing, selecting, and running the command library("haven") in your RMD file.
  1. Now, repeat the process above to install the here package.
    • Type install.packages("here") into your RMD file, hit RUN or select (highlight) this text line, and RUN the selection (Windows: CTRL + Enter; Mac: cmd + Enter).


  1. Once installed, load the here package by typing, selecting, and running library("here") into your RMD 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 RMarkdown code file in your top-level directory folder. For us, that means saving your RMD file in your CRIM5305_L_LastName folder.
      • Next, after closing RStudio, you will simply click directly on the RMD file in your CRIM5305_L_LastName 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 CRIM5305_L 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).

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


Part 4 (Assignment 1.4)

Goal: Save RMD; Use haven package to open SPSS file in RStudio

  1. In RStudio, click File > Save As, then save RMD file in CRIM5305_L_LastName folder. You should have already saved and named the file: YEAR-MO-DY_LastName_CRIM5305_Assign01
Save R Script and Markdown files in top-level folder

Save R Script and Markdown files in top-level folder

  1. It is essential that your RMD files are saved in the top-level CRIM5305_L_LastName folder for the here package to work properly.
  2. As in the picture above, your RMD should be in the same CRIM5305 folder as the “Datasets” folder
  3. After saving your RMD file, close RStudio, then reopen directly from RMD.
  1. Open CRIM5305_L folder, locate new RMD file (YEAR-MO-DY_LastName_CRIM5305Assign1_3RMD), and click or double-click to open with RStudio.
    • By opening RStudio directly from the RMD file saved in your CRIM5305_L folder, the here package by default will set the CRIM5305_L folder as the top-level working directory

  2. Add #comments to R code and load haven and here packages
    1. In your RMD, 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 RMD that are not commented out (i.e., both library commands), try running the entire document instead of only a single code chunk.
    3. Check the “Packages” tab to make sure there are checkmarks by the haven and here packages.

    4. Your RMD and RStudio session should now look similar to this:

RStudio session with comments and loaded packages

RStudio session with comments and loaded packages

  1. Note the red text in the Console that appeared after loading the here package. It confirms the here package has set my CRIM5305_L_LastName folder (i.e., for me, CRIM5305_L_Ducate) as the top-level working directory.


  1. Open SPSS dataset in RStudio using haven package and assign data to an object in R a.Start a second-level heading titled: “Read in and Assign Data to an Object.” Then, begin a new code chunk and type the following command in your RMD file: read_spss(here("Datasets","Monitoring the Future 2013 grade 10_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 Monitoring the Future 2013 grade 10_0.sav file in the “Datasets” folder to open.
      • Note: The file name you type in your code must EXACTLY match the name of your data file, including spaces and capital letters.
  2. Run the read_spss() command. You should see a small snapshot of the data in your R viewer (right-hand side of RStudio,) 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

  1. You just read a dataset into RStudio!

  2. 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 assign the dataset to an object in the R Environment. Note that your R Environment tab is still empty and looks like this:

Empty Environment after reading in SPSS data and before creating a data object

Empty Environment after reading in SPSS data and before creating a data object

  1. To assign the dataset to 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 machines like and Names humans like for more information and useful file naming tips.
    • Your RMD code should look like this:
Example RMD for reading SPSS Data into R Object

Example RMD for reading SPSS Data into R Object

  1. Run the line of code in your RMD to create the new data object.

Part 5

Goal: Submitting your first assignment

  1. After completing the first five parts of this assignment, hit ‘Knit’ in the top left (or press ‘Save’ if you selected ‘Knit on Save’). A Word document should pop up with all of your work. It should look like this:
Final Knitted Document for Assignment 1

Final Knitted Document for Assignment 1

  1. You should also have a Word documents in your “CRIM5305_L_LastName” folder with one (1) screenshot (i.e., local folder with 6 datafiles). Your folder should now look something like this:
Main Folder after Assignment 1 completion

Main Folder after Assignment 1 completion

  1. Log into Course Blackboard page, then go to “Course Content > Week 1” and open “Assignment 1.” Follow directions to upload and submit your documents for grading.

Assignment 1 Objective Checks

After completing assignment #1, …

  • have you created folders on your personal computer?
  • did you download 6 full datasets, then save them in your local storage folders?
  • do you know how to create a new R Markdown (RMD) document in RStudio?
  • do you know how to add and modify text, including italic or bold font and level headings, in an R Markdown document?
  • do you know how to add and use an R code chunk in an RMD file?
  • do you know how to use install.packages() and library() commands to install and load packages in R?
  • do you know how to add hashtags (#) to comment out a section of R code so it does not run?
  • are you able to use the here package and here() function for simple, reproducible file directory referencing?
  • are you able to read data into R/RStudio using read_spss() function from “haven” package, then assign it to an object in the R environment using an assignment (<-) operator?
  • are you able to knit your RMD file into a Word document that you can save and submit for course credit?