Chapter 5 Working with Factors
5.1 Manually recode/change a factor’s levels
Use forcats::fct_recode()
=diamonds %>% as_tibble()
diamonds
$cut=fct_recode(diamonds$cut, "meh"="Fair", "Wow"="Premium")
diamonds
summary(diamonds$cut)
## meh Good Very Good Wow Ideal
## 1610 4906 12082 13791 21551
5.2 Collapse factor levels
Extremely useful command for when you have infrequent cases in one factor and need to combine it with another.
Works by specifying a series of new level names, each of which contains the information from the old variables. Format is as follows:
fct_collapse(dataset$variable,
NewLevelA=c("OldLevel1","Oldlevel2"), # NewLevelA is the new variable that contains both variables 1 and 2
NewLevelB=c("OldLevel3"))
5.5 Change the order of a factor’s levels
=tribble(~person, ~condition,
example_data"bob", "25 years",
"jane", "5 years",
"jim", "5 years",
"john", "25 years")
$condition=factor(example_data$condition)
example_data
str(example_data$condition)
## Factor w/ 2 levels "25 years","5 years": 1 2 2 1
Notice that R thinks these are nominal factors, and that 25 comes before 5. To fix this and correct the level order…
$condition =fct_relevel(example_data$condition, c("5 years", "25 years")) # specify level order
example_data
str(example_data$condition)
## Factor w/ 2 levels "5 years","25 years": 2 1 1 2