Categorizing Data in R (Important Part of Job)
Mon Aug 28 2023 21:33:40 GMT+0000 (Coordinated Universal Time)
Saved by
@dr_dziedzorm
categorize_job <- function(description) {
if (grepl("communication|inform|listen", description, ignore.case = TRUE)) {
return("Communication : honest information dissemination")
} else if (grepl("safety|health|protect", description, ignore.case = TRUE)) {
return("Public Safety and Health: public protection")
} else if (grepl("policy|leadership|govern", description, ignore.case = TRUE)) {
return("Leadership and Governance: policy, implementation, decision-making")
} else if (grepl("resource|budget|manage", description, ignore.case = TRUE)) {
return("Resource Management: infrastructure, resource allocation")
} else if (grepl("hope|resilience|morale", description, ignore.case = TRUE)) {
return("Emotional and Psychological Well-being: hope, resilience, public morale")
} else {
return("Uncategorized")
}
}
my_data <- my_data %>%
mutate(Category = sapply(`What do you think is the single most important part of your job as a public official?`, categorize_job))
content_copyCOPY
Change "my_data" to name of imported data
Comments