Skip to content
/ forcats Public
forked from tidyverse/forcats

🐈🐈🐈🐈: tools for working with categorical variables (factors)

Notifications You must be signed in to change notification settings

wilkox/forcats

This branch is 1 commit ahead of, 222 commits behind tidyverse/forcats:main.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

382e52a Β· Feb 10, 2018
Feb 10, 2018
Aug 16, 2016
Aug 16, 2016
Feb 10, 2018
Feb 10, 2018
Jan 19, 2017
Feb 10, 2018
Feb 10, 2018
Feb 10, 2018
Aug 8, 2016
Feb 10, 2018
Feb 10, 2018
Jan 19, 2017
Feb 10, 2018
Feb 10, 2018
Feb 10, 2018
Apr 14, 2017
Feb 10, 2018
Jan 19, 2017
Aug 8, 2016

Repository files navigation

forcats

CRAN_Status_Badge Travis-CI Build Status Coverage Status

Overview

R uses factors to handle categorical variables, variables that have a fixed and known set of possible values. Historically, factors were much easier to work with than character vectors, so many base R functions automatically convert character vectors to factors. (For historical context, I recommend stringsAsFactors: An unauthorized biography by Roger Peng, and stringsAsFactors = <sigh> by Thomas Lumley. If you want to learn more about other approaches to working with factors and categorical data, I recommend Wrangling categorical data in R, by Amelia McNamara and Nicholas Horton.) These days, making factors automatically is no longer so helpful, so packages in the tidyverse never create them automatically.

However, factors are still useful when you have true categorical data, and when you want to override the ordering of character vectors to improve display. The goal of the forcats package is to provide a suite of useful tools that solve common problems with factors. If you’re not familiar with strings, the best place to start is the chapter on factors in R for Data Science.

Installation

# The easiest way to get forcats is to install the whole tidyverse:
install.packages("tidyverse")

# Alternatively, install just forcats:
install.packages("forcats")

# Or the the development version from GitHub:
# install.packages("devtools")
devtools::install_github("tidyverse/forcats")

Getting started

forcats is not part of the core tidyverse, so you need to load it explicitly:

library(tidyverse)
library(forcats)

Factors are used to describe categorical variables with a fixed and known set of levels. You can create factors with the base factor() or readr::parse_factor():

x1 <- c("Dec", "Apr", "Jan", "Mar")
month_levels <- c(
  "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
)

factor(x1, month_levels)
#> [1] Dec Apr Jan Mar
#> Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

parse_factor(x1, month_levels)
#> [1] Dec Apr Jan Mar
#> Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

The advantage of parse_factor() is that it will generate a warning if values of x are not valid levels:

x2 <- c("Dec", "Apr", "Jam", "Mar")

factor(x2, month_levels)
#> [1] Dec  Apr  <NA> Mar 
#> Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

parse_factor(x2, month_levels)
#> Warning: 1 parsing failure.
#> row # A tibble: 1 x 4 col     row   col expected           actual expected   <int> <int> <chr>              <chr>  actual 1     3    NA value in level set Jam
#> [1] Dec  Apr  <NA> Mar 
#> attr(,"problems")
#> # A tibble: 1 x 4
#>     row   col expected           actual
#>   <int> <int> <chr>              <chr> 
#> 1     3    NA value in level set Jam   
#> Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

Once you have the factor, forcats provides helpers for solving common problems.

About

🐈🐈🐈🐈: tools for working with categorical variables (factors)

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • R 100.0%