Assignment 2 Objectives

The purpose of this second assignment is to help you use R to complete some of the SPSS Exercises from the end of Chapter 1 in Bachman, Paternoster, & Wilson’s Statistics for Criminology & Criminal Justice, 5th Ed.

Following Assignment 1, you will create an R Markdown file in which to save and present your work for this class. Additionally, for this assignment, you will use R/RStudio to view variables in a data file and find information about variables, including variable names, labels, and values. This assignment should help you navigate R/RStudio and become comfortable finding basic information within data files.

By the end of assignment #2, you should be able to…

  • use view_df() function from the “sjPlot” package to quickly browse variables in a data file
  • use a tidyverse “pipe” (%>%) coding operator (from the “magrittr” package) to link together sequenced actions, such as call a data object then apply a function to a variable in that data object
  • use attr() function from base R to identify variable and attribute value labels
  • understand how the $ symbol can be used to call a specific element (e.g., a variable, row, or column) within an object (e.g., dataframe or tibble)
  • recognize when missing values are coded as NA for variables in your data file
  • recognize when a function is being called from a specific package using a double colon (i.e., with the package::function() format)
  • knit your RMD file into an HTML document and then save and submit it for course credit

Assumptions & Ground Rules

We are building on Assignment 1 objectives. By the start of this assignment, you should already know how to:

Basic R/RStudio skills

  • create an R Markdown (RMD) file and add/modify text, level headers, and R code chunks within it
  • install/load R packages and use hashtags (“#”) to comment out sections of R code so it does not run

Reproducibility

  • use here() for a simple and reproducible self-referential file directory method
  • Use groundhog.library() as an optional but recommended reproducible alternative to library() for loading packages


If you do not recall how to do these things, first review Assignment 1.

Additionally, you should have read the assigned book chapter and reviewed the SPSS questions that correspond to this assignment, and you should have completed any other course materials (e.g., videos; readings) assigned for this week before attempting this R assignment. In particular, for this week, I assume you understand:

  • difference between descriptive and inferential statistics
  • validity and reliability
  • difference between a population and a sample
  • sampling techniques

As noted previously, 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 2.1)

Goal: Create new R Markdown file in which to complete your Assignment 2.

(Note: Remember that, when following instructions, always substitute “LastName” for your own last name and substitute YEAR_MO_DY for the actual date. E.g., 2022_05_20_Fordham_K300Assign2_RMD)

In the first assignment, you learned how to create a new R Markdown file and use it to write and run R code, and make comments. You also saw how running certain commands (e.g., read_spss) from an R Markdown file will generate results in the RStudio Console and learned how to assign the results of such commands into an R object. In Assignment 2, you will learn how to read in and assign datasets as an R object. You will also learn how to use the sjPlot package to quickly view variables with its view_df() function and to use the base R attr() function to identify variable labels and variable attribute value labels.

  1. Go to your K300_L folder, which should contain the R Markdown file you created for Assignment 1 (named YEAR_MO_DY_LastName_K300Assign1_3RMD). Click to open the R Markdown file.
    • Remember, we open RStudio like this because it is an easy way to ensure the here package automatically sets our K300_L folder as the top-level working directory.

  2. In RStudio, open a new R Markdown document. If you do not recall how to do this, refer to Assignment 1.

  3. 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 K300 Assignment 2.
    2. In the Author box, enter your First and Last Name (e.g., Jon Brauer & Tyeisha Fordham).
    3. Under Default Output Format box, select “HTML” (HTML is usually the default selection)
      • (Note: In the first assignment, you knitted to a Word document. For this and remaining assignments, you will use the default setting to knit to HTML documents instead)

  4. Remember that 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. Be sure to delete this text before you begin working.

Part 2 (Assignment 2.2)

Goal: Read data and assign to R object.

  1. Create a second-level header titled: “Part 1 (Assignment 2.1).” Then, create a third-level header titled: “Learning R Markdown.”

  2. This assignment must be completed by the student and the student alone. To confirm that this is your work, please begin all assignments with this text: This R Markdown document contains my work for Assignment 2. It is my work and only my work.

  3. Now, you need to get data into RStudio. You already know how to do this, but please refer to Assignment 1 if you have questions.
    1. Create a second-level header in R Markdown (hereafter, “RMD”) file titled: “Load Libraries”
    2. Insert an R chunk
    3. Inside the new R code chunk, load the following three packages: tidyverse, haven, and here.
      • Recall, you only need to install packages one time. However, you must load them each time you start a new R session.
      • Also, if you did not optionally install the “tidyverse” package for Assignment 1 (because it includes the “haven” package), then you will need to install that package before loading it. you can do this by typing install.packages("tidyverse") in the R console. Alternatively, you can type that into an R chunk - just remember to comment out the command after running it (by adding a “#” in front of it).

  4. After your first code chunk, create another second-level header in RMD titled: “Read Data into R”
    1. Insert another R code chunk
    2. In the new R code chunk, read and assign the “2013YRBS.sav” SPSS datafile into an R data object named YRBS2013data.
      • Forget how to do this? Refer to instructions in Assignment 1.
    3. In the same code chunk, on a new line below your read data/assign object command, type the name of your new R data object: YRBS2013data. This will call the object and provide a brief view of the data. (Note: You can get a similar but more visually appealing view by simply clicking on the object in the “Environment” window. More on this later.) Your R studio session should now look a lot like this: