One way is to make it approachable via tidyr::separate()
. This depends on you knowing all possible values for your factor
variable.
library(tidyverse)
original_df <- tribble(
~variable, ~response,
"JobFactorDiversity", "Important",
"JobFactorRemote", "Not Important",
"JobFactorDiversity", "Not Important",
"WorkChallengeFrequency", "Frequent",
"WorkChallengeDirtyData", "Rarely"
)
prefixes <- c("JobFactor", "WorkChallenge")
prefixes <- paste(prefixes, collapse = "|")
prefixes <- paste0("(", prefixes, ")")
original_df %>%
mutate(variable = sub(prefixes, "\\1|", variable)) %>%
separate(variable, into = c("factor", "aspect"))
#> # A tibble: 5 x 3
#> factor aspect response
#> * <chr> <chr> <chr>
#> 1 JobFactor Diversity Important
#> 2 JobFactor Remote Not Important
#> 3 JobFactor Diversity Not Important
#> 4 WorkChallenge Frequency Frequent
#> 5 WorkChallenge DirtyData Rarely
Created on 2018-01-21 by the reprex package (v0.1.1.9000).