All Projects β†’ tidyverse β†’ Forcats

tidyverse / Forcats

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

Programming Languages

r
7636 projects

Labels

Projects that are alternatives of or similar to Forcats

R4Econ
R Code Examples Multi-dimensional/Panel Data
Stars: ✭ 16 (-96.37%)
Mutual labels:  tidyverse
tidytree
🚿A Tidy Tool for Phylogenetic Tree Data Manipulation
Stars: ✭ 34 (-92.29%)
Mutual labels:  tidyverse
Statistical rethinking with brms ggplot2 and the tidyverse
The bookdown version lives here: https://bookdown.org/content/3890
Stars: ✭ 350 (-20.63%)
Mutual labels:  tidyverse
tbltools
πŸ—œπŸ”’ Tools for Working with Tibbles
Stars: ✭ 34 (-92.29%)
Mutual labels:  tidyverse
resamplr
R package cross-validation, bootstrap, permutation, and rolling window resampling techniques for the tidyverse.
Stars: ✭ 35 (-92.06%)
Mutual labels:  tidyverse
r4dswebsite
Public repository for the R4DS community website.
Stars: ✭ 19 (-95.69%)
Mutual labels:  tidyverse
datar
A Grammar of Data Manipulation in python
Stars: ✭ 142 (-67.8%)
Mutual labels:  tidyverse
Tidytuesday
πŸ“Š My contributions to the #TidyTuesday challenge
Stars: ✭ 410 (-7.03%)
Mutual labels:  tidyverse
parcours-r
Valise pΓ©dagogique pour la formation Γ  R
Stars: ✭ 25 (-94.33%)
Mutual labels:  tidyverse
Tidy
Tidy up your data with JavaScript, inspired by dplyr and the tidyverse
Stars: ✭ 307 (-30.39%)
Mutual labels:  tidyverse
desctable
An R package to produce descriptive and comparative tables
Stars: ✭ 49 (-88.89%)
Mutual labels:  tidyverse
casewhen
Create reusable dplyr::case_when() functions
Stars: ✭ 64 (-85.49%)
Mutual labels:  tidyverse
chilemapas
Mapas terrestres de Chile con topologias simplificadas.
Stars: ✭ 23 (-94.78%)
Mutual labels:  tidyverse
tidyweek
Repo dedicated to #tidyweek & Mentorship pilot
Stars: ✭ 25 (-94.33%)
Mutual labels:  tidyverse
Timetk
A toolkit for working with time series in R
Stars: ✭ 371 (-15.87%)
Mutual labels:  tidyverse
advanced-data-wrangling-in-R-legacy
Advanced-data-wrangling-in-R, Workshop
Stars: ✭ 14 (-96.83%)
Mutual labels:  tidyverse
datatoolbox
πŸŽ“ Data Toolbox Training
Stars: ✭ 18 (-95.92%)
Mutual labels:  tidyverse
Tidylog
Tidylog provides feedback about dplyr and tidyr operations. It provides wrapper functions for the most common functions, such as filter, mutate, select, and group_by, and provides detailed output for joins.
Stars: ✭ 428 (-2.95%)
Mutual labels:  tidyverse
Tidygraph
A tidy API for graph manipulation
Stars: ✭ 398 (-9.75%)
Mutual labels:  tidyverse
rfordatasciencewiki
Resources for the R4DS Online Learning Community, including answer keys to the text
Stars: ✭ 40 (-90.93%)
Mutual labels:  tidyverse

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("devtools")
devtools::install_github("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 x 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
#> # … with 27 more rows
starwars %>%
  filter(!is.na(species)) %>%
  mutate(species = fct_lump(species, n = 3)) %>%
  count(species)
#> # A tibble: 4 x 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.

Code of Conduct

Please note that the β€˜forcats’ project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].