0

how can I group elements of a variable by common patterns. for example I have a database in which there is a field called company role and I would like to be able to group the common roles into one.

employee <- c("a", "b", "c", "d", "e")
Rol      <- c(" accounting assistant", "accou assist", "account.assistant", 
              "healt aux", "auxiliary in healt")
DF <- data.frame(employee, Rol)

I would like to transform it into something like this

Employeee ROL
A accounting assistant
B accounting assistant
C accounting assistant
D Healt auxiliary
E Healt auxiliary

At the moment i'm manually indentifying the patterns but as the data grows the task becomes more complex, i'd appreciate any help. Thanks!

jay.sf
  • 60,139
  • 8
  • 53
  • 110
MatCordTo
  • 223
  • 1
  • 7

1 Answers1

0
library(dplyr)
# your dataframe
employee <- c("a", "b", "c", "d", "e")
Rol      <- c(" accounting assistant", "accou assist", "account.assistant", 
              "healt aux", "auxiliary in healt")
DF <- data.frame(employee, Rol)

# save the terms you want to unify in vectors
vector_accounting <- c(" accounting assistant", "accou assist", "account.assistant")
vector_healt <- c("healt aux", "auxiliary in healt")

# apply changes with %in%
DF1 <- DF %>% 
  mutate(Rol = case_when(Rol %in% vector_accounting ~ "accounting assistent",
                         Rol %in% vector_healt ~ "Heat auxiliary"))

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66