• Stars
    star
    522
  • Rank 77,711 (Top 2 %)
  • Language
    R
  • License
    Other
  • Created over 7 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

forcats

CRAN status R-CMD-check Codecov test coverage

Overview

R uses factors to handle categorical variables, variables that have a fixed and known set of possible values. Factors are also helpful for reordering character vectors to improve display. The goal of the forcats package is to provide a suite of tools that solve common problems with factors, including changing the order of levels or the values. Some examples include:

  • fct_reorder(): Reordering a factor by another variable.
  • fct_infreq(): Reordering a factor by the frequency of values.
  • fct_relevel(): Changing the order of a factor by hand.
  • fct_lump(): Collapsing the least/most frequent values of a factor into β€œother”.

You can learn more about each of these in vignette("forcats"). If you’re new to factors, 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("pak")
pak::pak("tidyverse/forcats")

Cheatsheet

Getting started

forcats is part of the core tidyverse, so you can load it with library(tidyverse) or library(forcats).

library(forcats)
library(dplyr)
library(ggplot2)
starwars %>% 
  filter(!is.na(species)) %>%
  count(species, sort = TRUE)
#> # A tibble: 37 Γ— 2
#>    species      n
#>    <chr>    <int>
#>  1 Human       35
#>  2 Droid        6
#>  3 Gungan       3
#>  4 Kaminoan     2
#>  5 Mirialan     2
#>  6 Twi'lek      2
#>  7 Wookiee      2
#>  8 Zabrak       2
#>  9 Aleena       1
#> 10 Besalisk     1
#> # β„Ή 27 more rows
starwars %>%
  filter(!is.na(species)) %>%
  mutate(species = fct_lump(species, n = 3)) %>%
  count(species)
#> # A tibble: 4 Γ— 2
#>   species     n
#>   <fct>   <int>
#> 1 Droid       6
#> 2 Gungan      3
#> 3 Human      35
#> 4 Other      39
ggplot(starwars, aes(x = eye_color)) + 
  geom_bar() + 
  coord_flip()

starwars %>%
  mutate(eye_color = fct_infreq(eye_color)) %>%
  ggplot(aes(x = eye_color)) + 
  geom_bar() + 
  coord_flip()

More resources

For a history of factors, 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.

Getting help

If you encounter a clear bug, please file a minimal reproducible example on Github. For questions and other discussion, please use community.rstudio.com.

More Repositories

1

ggplot2

An implementation of the Grammar of Graphics in R
R
6,163
star
2

dplyr

dplyr: A grammar of data manipulation
R
4,556
star
3

tidyverse

Easily install and load packages from the tidyverse
R
1,480
star
4

rvest

Simple web scraping for R
R
1,428
star
5

tidyr

Tidy Messy Data
R
1,298
star
6

purrr

A functional programming toolkit for R
R
1,185
star
7

readr

Read flat files (csv, tsv, fwf) into R
R
971
star
8

magrittr

Improve the readability of R code with the pipe
R
942
star
9

datascience-box

Data Science Course in a Box
JavaScript
852
star
10

reprex

Render bits of R code for sharing, e.g., on GitHub or StackOverflow.
R
716
star
11

lubridate

Make working with dates in R just that little bit easier
R
705
star
12

readxl

Read excel files (.xls and .xlsx) into R πŸ–‡
C++
699
star
13

dtplyr

Data table backend for dplyr
R
639
star
14

multidplyr

A dplyr backend that partitions a data frame over multiple processes
R
632
star
15

tibble

A modern re-imagining of the data frame
R
623
star
16

vroom

Fast reading of delimited files
C++
589
star
17

stringr

A fresh approach to string manipulation in R
R
555
star
18

dbplyr

Database (DBI) backend for dplyr
R
416
star
19

haven

Read SPSS, Stata and SAS files from R
C
414
star
20

modelr

Helper functions for modelling
R
398
star
21

googlesheets4

Google Spreadsheets R API (reboot of the googlesheets package)
R
330
star
22

googledrive

Google Drive R API
R
297
star
23

style

The tidyverse style guide for R code
HTML
275
star
24

design

Tidyverse design principles
R
203
star
25

tidyverse.org

Source of tidyverse.org
HTML
172
star
26

hms

A simple class for storing time-of-day values
R
134
star
27

nycflights13

An R data package containing all out-bound flights from NYC in 2013 + useful metdata
R
116
star
28

tidyversedashboard

Tidyverse activity dashboard
R
69
star
29

tidy-dev-day

Tidyverse developer day
57
star
30

tidyeval

A guide to tidy evaluation
CSS
51
star
31

dsbox

Companion R package to Data Science Course in a Box
R
44
star
32

blob

A simple S3 class for representing BLOBs
R
42
star
33

tidytemplate

A pkgdown template for core tidyverse packages
SCSS
42
star
34

funs

Collection of low-level functions for working with vctrs
R
30
star
35

website-analytics

Web analytics for tidyverse + r-lib sites
R
27
star
36

code-review

23
star
37

tidyups

19
star
38

ggplot2-docs

ggplot2 documentation. Auto-generated from ggplot2 sources by pkgdown
HTML
9
star