Grouping 2 levels of a factor in R

R

R Problem Overview


I have a column of data that is a factor with levels A, B and C, I am interested in combining two of these levels into one factor, so it would become A and B, with B = B and C, or maybe a new variable A and D, with D = B and C. I can come up with plenty of ways to do this by looping through the column with if statements, but I feel like there should be a more elegant approach and I was wondering if someone could point me in the right direction.

R Solutions


Solution 1 - R

Use levels(x) <- ... to specify new levels, and to combine some previous levels. For example:

f <- factor(LETTERS[c(1:3, 3:1)])
f
[1] A B C C B A
Levels: A B C

Now combine "A" and "B" into a single level:

levels(f) <- c("A", "A", "C")
f
[1] A A C C A A
Levels: A C

Solution 2 - R

If you're using dplyr pipes you can use the forcats package.

library(forcats)
f %>% fct_collapse(A = c("A","B"))

#[1] A A C C A A
#Levels: A C

Solution 3 - R

The rockchalk library is able to combine levels. I think its great, if you want to combine B and C together in a factor do this:

library(rockchalk)
combineLevels(mydf$facVar,levs = c("B", "C"), newLabel = c("BandC") )

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionasjohnsonView Question on Stackoverflow
Solution 1 - RAndrieView Answer on Stackoverflow
Solution 2 - RJoeView Answer on Stackoverflow
Solution 3 - RdeboView Answer on Stackoverflow