Package: forcats


Function: as_factor()


1. Convert a character variable (Var1) to a factor

Review the data (d2)

# A tibble: 3 x 5
  Var1   Var2 Var3  Var4       Var5 
  <chr> <int> <chr> <chr>      <lgl>
1 b         2 3.6   10/10/2004 TRUE 
2 a        NA 8.5   12/14/2007 FALSE
3 c         3 X     08/09/2020 TRUE 

View the class for Var1

class(d2$Var1)
[1] "character"

Convert Var1 to a factor.

  • Note: In the case of a character variable, the levels will be chosen based on the order in which they appear. In the case of a numeric variable, the levels will be numerically ordered. You can view levels using the base::levels() function.

  • Note: We are recoding into a new variable using dplyr::mutate() and saving over the original variable by naming the new variable the same name as the original.

d2 <- d2 %>% 
  dplyr::mutate(Var1 = forcats::as_factor(Var1))

class(d2$Var1)
[1] "factor"
levels(d2$Var1)
[1] "b" "a" "c"
  • Note: If you donโ€™t want the levels as they are chosen, you can use the function forcats::fct_relevel() instead to convert your variable to a factor and assign your level order. However, this function will only work if your variable is a character or factor variable.
d2 <- d2 %>% 
  dplyr::mutate(Var1 = forcats::fct_relevel(Var1, "a", "b", "c"))

class(d2$Var1)
[1] "factor"
levels(d2$Var1)
[1] "a" "b" "c"

Package: base


Function: as.factor()


1. Convert a character variable (Var1) to a factor

Review the data (d2)

# A tibble: 3 x 5
  Var1   Var2 Var3  Var4       Var5 
  <chr> <int> <chr> <chr>      <lgl>
1 b         2 3.6   10/10/2004 TRUE 
2 a        NA 8.5   12/14/2007 FALSE
3 c         3 X     08/09/2020 TRUE 

View the class for Var1

class(d2$Var1)
[1] "character"

Convert Var1 to a factor.

  • Note: The levels will be chosen alphabetically or numerically. You can view levels using the base::levels() function.
d2 <- d2 %>% 
  dplyr::mutate(Var1 = as.factor(Var1))

class(d2$Var1)
[1] "factor"
levels(d2$Var1)
[1] "a" "b" "c"

However, if you want to assign the levels when converting to a factor, you can use the function base::factor() instead with the argument levels.

d2 <- d2 %>% 
  dplyr::mutate(Var1 = factor(Var1, levels = c("c", "b", "a")))

class(d2$Var1)
[1] "factor"
levels(d2$Var1)
[1] "c" "b" "a"

Return to Data Types