{"id":13665965,"url":"https://github.com/tidyverse/forcats","last_synced_at":"2025-04-26T09:31:53.667Z","repository":{"id":10291311,"uuid":"65227405","full_name":"tidyverse/forcats","owner":"tidyverse","description":"🐈🐈🐈🐈: tools for working with categorical variables (factors)","archived":false,"fork":false,"pushed_at":"2024-08-09T18:46:29.000Z","size":12036,"stargazers_count":554,"open_issues_count":39,"forks_count":127,"subscribers_count":21,"default_branch":"main","last_synced_at":"2025-04-22T10:21:57.587Z","etag":null,"topics":["factor","r","tidyverse"],"latest_commit_sha":null,"homepage":"https://forcats.tidyverse.org/","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tidyverse.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":".github/SUPPORT.md","governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-08T18:07:47.000Z","updated_at":"2025-04-15T14:06:25.000Z","dependencies_parsed_at":"2023-02-16T08:46:13.825Z","dependency_job_id":"9afa9b26-3967-4ba9-a3e8-6b0e689350ef","html_url":"https://github.com/tidyverse/forcats","commit_stats":{"total_commits":350,"total_committers":52,"mean_commits":6.730769230769231,"dds":"0.24285714285714288","last_synced_commit":"6a9bd65915502655a2ac025b93235964fc0d194c"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidyverse%2Fforcats","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidyverse%2Fforcats/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidyverse%2Fforcats/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidyverse%2Fforcats/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tidyverse","download_url":"https://codeload.github.com/tidyverse/forcats/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250300608,"owners_count":21407956,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["factor","r","tidyverse"],"created_at":"2024-08-02T06:00:54.858Z","updated_at":"2025-04-26T09:31:53.058Z","avatar_url":"https://github.com/tidyverse.png","language":"R","funding_links":[],"categories":["R","Table of Contents"],"sub_categories":["Data manipulation"],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\"\n)\n```\n\n# forcats \u003cimg src='man/figures/logo.png' align=\"right\" height=\"139\" /\u003e\n\n\u003c!-- badges: start --\u003e\n[![CRAN status](https://www.r-pkg.org/badges/version/forcats)](https://cran.r-project.org/package=forcats)\n[![R-CMD-check](https://github.com/tidyverse/forcats/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/tidyverse/forcats/actions/workflows/R-CMD-check.yaml)\n[![Codecov test coverage](https://codecov.io/gh/tidyverse/forcats/branch/main/graph/badge.svg)](https://app.codecov.io/gh/tidyverse/forcats?branch=main)\n\u003c!-- badges: end --\u003e\n\n## Overview\n\nR 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: \n\n* `fct_reorder()`: Reordering a factor by another variable.\n* `fct_infreq()`: Reordering a factor by the frequency of values.\n* `fct_relevel()`: Changing the order of a factor by hand.\n* `fct_lump()`: Collapsing the least/most frequent values of a factor into \"other\".\n\nYou 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](https://r4ds.hadley.nz/factors.html) in R for Data Science.\n\n## Installation\n\n```\n# The easiest way to get forcats is to install the whole tidyverse:\ninstall.packages(\"tidyverse\")\n\n# Alternatively, install just forcats:\ninstall.packages(\"forcats\")\n\n# Or the the development version from GitHub:\n# install.packages(\"pak\")\npak::pak(\"tidyverse/forcats\")\n```\n\n## Cheatsheet\n\n\u003ca href=\"https://raw.githubusercontent.com/rstudio/cheatsheets/main/factors.pdf\"\u003e\u003cimg src=\"https://github.com/rstudio/cheatsheets/raw/main/pngs/thumbnails/forcats-cheatsheet-thumbs.png\" width=\"320\" height=\"252\"/\u003e\u003c/a\u003e\n\n## Getting started\n\nforcats is part of the core tidyverse, so you can load it with `library(tidyverse)` or `library(forcats)`.\n\n```{r setup, message = FALSE}\nlibrary(forcats)\nlibrary(dplyr)\nlibrary(ggplot2)\n```\n\n```{r}\nstarwars %\u003e% \n  filter(!is.na(species)) %\u003e%\n  count(species, sort = TRUE)\n```\n\n```{r}\nstarwars %\u003e%\n  filter(!is.na(species)) %\u003e%\n  mutate(species = fct_lump(species, n = 3)) %\u003e%\n  count(species)\n```\n\n```{r unordered-plot}\nggplot(starwars, aes(x = eye_color)) + \n  geom_bar() + \n  coord_flip()\n```\n\n```{r ordered-plot}\nstarwars %\u003e%\n  mutate(eye_color = fct_infreq(eye_color)) %\u003e%\n  ggplot(aes(x = eye_color)) + \n  geom_bar() + \n  coord_flip()\n```\n\n## More resources \n\nFor a history of factors, I recommend [_stringsAsFactors: An unauthorized biography_](https://simplystats.github.io/2015/07/24/stringsasfactors-an-unauthorized-biography/) by Roger Peng and [_stringsAsFactors = \\\u003csigh\\\u003e_](https://notstatschat.tumblr.com/post/124987394001/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_](https://peerj.com/preprints/3163/), by Amelia McNamara and Nicholas Horton. \n\n## Getting help\n\nIf you encounter a clear bug, please file a minimal reproducible example on [Github](https://github.com/tidyverse/forcats/issues). For questions and other discussion, please use [community.rstudio.com](https://community.rstudio.com/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftidyverse%2Fforcats","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftidyverse%2Fforcats","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftidyverse%2Fforcats/lists"}