Suppose I have a categorical variable such like:
set.seed(123)
x<-sample(c("I", "IA", "IB", "II", "IIB", "IIC", "III", "IIID", "IIIF", "XA", "XB", "XC"),
100, TRUE)
table(x, exclude=NULL)
# x
# I IA IB II IIB IIC III IIID IIIF XA XB XC <NA>
# 5 12 9 7 9 11 6 8 6 12 9 6 0
My question is how to easily collapse x into four elements, e.g. I, II, III and X? E.g. combining I, IA, IB into I etc.
More generally, if your categorical variables aren't grouped by such patterns, you can specify a mapping using case_when from dplyr:
y <- case_when(x %in% c("I", "IA", "IB") ~ "I", #or whatever conditions you want
x %in% c("II", "IIA", "IIB") ~ "II", #as above
TRUE ~ "III")
table(y)
I II III
33 24 43
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With