Learn R Programming

compstatr (version 0.2.1)

cs_crime_cat: Categorize Crime

Description

The SLMPD data contains 5 or 6 digit codes to refer to specific categories of crime. cs_crime_cat transforms these into either string, factor, or simplified numeric categories like "murder" or "aggravated assault". This can be used on any police department's data where codes like 31111 (robbery with a firearm) or 142320 (malicious destruction of property) are used to identify crimes.

Usage

cs_crime_cat(.data, var, newVar, output)

Arguments

.data

A tibble or data frame

var

Name of variable with 5 or 6 digit crime codes

newVar

Name of output variable to be created with simplified categories

output

Type of output - either "string", "factor", or "numeric". If "numeric" is selected, the general UCR code will be returned (i.e. 1 for homicide, 3 for aggravated assault, etc.). Factor output will be returned in order of descending UCR code (i.e. beginning with homicide, which has a UCR code of 1).

Value

A copy of the object with the new output variable appended to it.

Details

The categories used here are derived from the U.S. Federal Bureau of Investigation's Uniform Crime Reporting codes.

Examples

Run this code
# NOT RUN {
# load example data
testData <- january2018

# apply categories
testData <- cs_crime_cat(testData,var = crime, newVar = crimeCat, output = "numeric")

# preview categories
table(testData$crimeCat)

# apply categories
testData <- cs_crime_cat(testData,var = crime, newVar = crimeCat, output = "factor")

# preview categories
table(testData$crimeCat)

# apply categories
testData <- cs_crime_cat(testData,var = crime, newVar = crimeCat, output = "string")

# preview categories
table(testData$crimeCat)

# }

Run the code above in your browser using DataLab