-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathREADME.Rmd
74 lines (51 loc) · 2.98 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
# forcats <img src="logo.png" align="right" />
[](https://cran.r-project.org/package=forcats)
[](https://travis-ci.org/tidyverse/forcats)
[](https://codecov.io/github/tidyverse/forcats?branch=master)
## 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 more historical context, I recommend [_stringsAsFactors: An unauthorized biography_](http://simplystatistics.org/2015/07/24/stringsasfactors-an-unauthorized-biography/) by Roger Peng, and [_stringsAsFactors = \<sigh\>_](http://notstatschat.tumblr.com/post/124987394001/stringsasfactors-sigh) by Thomas Lumley.) These days, making factors automatically is longer so helpful, so packages in the [tidyverse](http://tidyverse.org) 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](http://r4ds.had.co.nz/factors.html) in R for Data Science.
## Installation
```R
# 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:
```{r setup, message = FALSE}
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()`](http://readr.tidyverse.org/reference/parse_factor.html):
```{r}
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)
parse_factor(x1, month_levels)
```
The advantage of `parse_factor()` is that it will generate a warning if values of `x` are not valid levels:
```{r}
x2 <- c("Dec", "Apr", "Jam", "Mar")
factor(x2, month_levels)
parse_factor(x2, month_levels)
```
Once you have the factor, forcats provides helpers for solving common problems.