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.
view_df() function from the “sjPlot” package to
quickly browse variables in a data file
%>%) 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
attr() function from base R to identify variable
and attribute value labels
$ symbol can be used to call a
specific element (e.g., a variable, row, or column) within an object
(e.g., dataframe or tibble)
variables in your data file
We are building on Assignment 1 objectives. By the start of this assignment, you should already know how to:
here() for a simple and reproducible
self-referential file directory method
groundhog.library() as an optional but recommended
reproducible alternative to
library() for loading
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:
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).
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
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.
here package automatically sets our K300_L
folder as the top-level working directory.
Goal: Read data and assign to R object.
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
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: