Tidyverse Book contribution by Rickidon Singh: forcats

The forcats package

forcats is a non-core package in the tidyverse and all the main functions start with fct_ Although you can install forcats using install.packages(“tidyverse”), it is loaded using library(forcats).

Load necessary libraries

library(forcats)

as_factor (): Converts an input to a factor. as_factor() is a generic version of as.factor()

Description: Creates levels in the order in which they appear. This ensures means that as_factor(x) will always return the same result, regardless of the current locale.

Usage:

as_factor(x, ...)

Example:

x <- c("e", "f", "g")
as_factor(x)
## [1] e f g
## Levels: e f g
as.factor(x)
## [1] e f g
## Levels: e f g

fct_c (): Concatenate factors, combining levels.

Description: Allows the patching of factors having both different levels and are of different sources.

Usage:

fct_c(...)

Example:

fr <- factor("r")
fs <- factor("s")
frs <- factor(c("r", "s"))

c(fr, fs, frs)
## [1] 1 1 1 2
fct_c(fr, fs, frs)
## [1] r s r s
## Levels: r s

fct_count (): Count entries in a factor.

Description: Count entries in a factor.

Usage:

fct_count(f, sort = FALSE)

Example:

f <- factor(sample(letters)[rpois(1000, 10)])
table(f)
## f
##   a   b   c   d   e   f   g   h   i   j   k   m   p   q   r   s   v   w 
## 155   6  92  48  35 115  29   4  73 100  45 121  14  31  93   2   1   6 
##   x   y 
##   6  24
fct_count(f)
## # A tibble: 20 x 2
##    f         n
##    <fct> <int>
##  1 a       155
##  2 b         6
##  3 c        92
##  4 d        48
##  5 e        35
##  6 f       115
##  7 g        29
##  8 h         4
##  9 i        73
## 10 j       100
## 11 k        45
## 12 m       121
## 13 p        14
## 14 q        31
## 15 r        93
## 16 s         2
## 17 v         1
## 18 w         6
## 19 x         6
## 20 y        24
fct_count(f, sort = TRUE)
## # A tibble: 20 x 2
##    f         n
##    <fct> <int>
##  1 a       155
##  2 m       121
##  3 f       115
##  4 j       100
##  5 r        93
##  6 c        92
##  7 i        73
##  8 d        48
##  9 k        45
## 10 e        35
## 11 q        31
## 12 g        29
## 13 y        24
## 14 p        14
## 15 b         6
## 16 w         6
## 17 x         6
## 18 h         4
## 19 s         2
## 20 v         1

fct_drop (): ALlows you to drop unused levels.

Description: In comparison to base::droplevels(), does not drop NA levels that have values.

Usage:

fct_drop(f, only)

Example:

f <- factor(c("a", "b"), levels = c("a", "b", "c"))
f
## [1] a b
## Levels: a b c
fct_drop(f)
## [1] a b
## Levels: a b
#Restrict which levels to drop
fct_drop(f, only = "a")
## [1] a b
## Levels: a b c
fct_drop(f, only = "c")
## [1] a b
## Levels: a b

fct_expand (): Add additional levels to a factor.

Description: Allows the expansion of levels to a chosen factor.

Usage:

fct_expand(f, ...)

Example:

f <- factor(sample(letters[1:3], 20, replace = TRUE))
f
##  [1] b b a c a c b a c a c a c b b c c c b c
## Levels: a b c
fct_expand(f, "d", "e", "f")
##  [1] b b a c a c b a c a c a c b b c c c b c
## Levels: a b c d e f
fct_expand(f, letters[1:6])
##  [1] b b a c a c b a c a c a c b b c c c b c
## Levels: a b c d e f

fct_explicit_na (): Make missing values explicit.

Description: Provides missing value an explicit factor level, ensuring that they appear in summaries and on plots.

Usage:

fct_explicit_na(f, na_level = "(Missing)")

Example:

f1 <- factor(c("a", "a", NA, NA, "a", "a", NA, NA, "c", "a", "c", "b"))
table(f1)
## f1
## a b c 
## 5 1 2
f2 <- fct_explicit_na(f1)
table(f2)
## f2
##         a         b         c (Missing) 
##         5         1         2         4

fct_inorder (): Reorder factors levels by first appearance or frequency.

Description: Reorders the factor levels by first appearance or frequency.

Usage:

fct_inorder(f, ordered = NA)

fct_infreq(f, ordered = NA)

Example:

f <- factor(c("b", "b", "a", "c", "c", "c"))
f
## [1] b b a c c c
## Levels: a b c
fct_inorder(f)
## [1] b b a c c c
## Levels: b a c
fct_infreq(f)
## [1] b b a c c c
## Levels: c b a
fct_inorder(f, ordered = TRUE)
## [1] b b a c c c
## Levels: b < a < c

fct_other (): Replaces levels with “other.”

Description: Makes it easier to convert selected levels to “other”

Usage:

fct_other(f, keep, drop, other_level = "Other")

Example:

x <- factor(rep(LETTERS[1:9], times = c(40, 10, 5, 27, 1, 1, 1, 1, 1)))
fct_other(x, keep = c("A", "B"))
##  [1] A     A     A     A     A     A     A     A     A     A     A    
## [12] A     A     A     A     A     A     A     A     A     A     A    
## [23] A     A     A     A     A     A     A     A     A     A     A    
## [34] A     A     A     A     A     A     A     B     B     B     B    
## [45] B     B     B     B     B     B     Other Other Other Other Other
## [56] Other Other Other Other Other Other Other Other Other Other Other
## [67] Other Other Other Other Other Other Other Other Other Other Other
## [78] Other Other Other Other Other Other Other Other Other Other
## Levels: A B Other
fct_other(x, drop = c("A", "B", "C", "D"))
##  [1] Other Other Other Other Other Other Other Other Other Other Other
## [12] Other Other Other Other Other Other Other Other Other Other Other
## [23] Other Other Other Other Other Other Other Other Other Other Other
## [34] Other Other Other Other Other Other Other Other Other Other Other
## [45] Other Other Other Other Other Other Other Other Other Other Other
## [56] Other Other Other Other Other Other Other Other Other Other Other
## [67] Other Other Other Other Other Other Other Other Other Other Other
## [78] Other Other Other Other Other E     F     G     H     I    
## Levels: E F G H I Other

fct_recode (): Change factor levels by hand.

Description: Allows the change of factor levels by hand.

Usage:

fct_recode(.f, ...)

Example:

x <- factor(c("canada", "dog", "brazil", "cat"))
fct_recode(x, country = "canada", country = "brazil")
## [1] country dog     country cat    
## Levels: country cat dog
# Keep in mind, if you name the level NULL it will be removed
fct_recode(x, NULL = "canada", country = "brazil")
## [1] <NA>    dog     country cat    
## Levels: country cat dog

fct_relabel(): ALlows the programmatic relabeling of levels.

Description: Automatically relabel factor levels, collapse as necessary.

Usage:

fct_relabel(.f, .fun, ...)

Example:

x <- factor(letters[1:3])
x
## [1] a b c
## Levels: a b c
x %>% fct_relabel(function(x) paste0("-", x, "-"))
## [1] -a- -b- -c-
## Levels: -a- -b- -c-

fct_relevel (): Reorder factor levels by hand.

Description: Allows you to move any number of levels to any location.

Usage:

fct_relevel(.f, ..., after = 0L)

Example:

f <- factor(c("w", "x", "y", "z"))
fct_relevel(f)
## [1] w x y z
## Levels: w x y z
fct_relevel(f, "y")
## [1] w x y z
## Levels: y w x z
fct_relevel(f, "x", "w")
## [1] w x y z
## Levels: x w y z
# Move to the forth position
fct_relevel(f, "x", after = 3)
## [1] w x y z
## Levels: w y z x
# 'Inf' allows you to relevel to the end when there is an unknown number of levels
fct_relevel(f, "w", after = Inf)
## [1] w x y z
## Levels: x y z w