Hi! Welcome! 
Take a look at the forcats
package (tidyverse tools for working with factor data, like yours), especially the functions fct_collapse
and fct_relabel
. You can see an overview of all the functions here.
fct_relabel
will be especially helpful if there are patterns within the codes — such as “all codes with first two digits in these ranges => violent”, since you can put that logic into a function to relabel lots of codes automatically.
As a very simple example, based on the SSA website’s list of violent and drug-related codes: 09XX, 10XX, 11XX, 12XX, 13XX, 16XX, 21XX, 52XX, you could write a function like:
library(tidyverse)
library(stringr)
library(forcats)
assign_violent <- function(code) {
if(str_sub(code, 2) %in% c("09", "10", "11", "12", "13", "16", "21", "52")) {
“violent”
} else {
code
}
}
# suppose a dataframe df with a factor variable crime_code...
df %>% mutate(crime_code = fct_relabel(crime_code, assign_violent))
Does that help get you started? To give more specific advice, it will be helpful to see the code you’re working with (it’s ok if it doesn’t work right!), made as reproducible as possible. If you want to learn about how things work around here, definitely take a look at the FAQs!