https://github.com/jonmcalder/refactor
:bookmark: Better factor handling for R
https://github.com/jonmcalder/refactor
factor package r
Last synced: 7 months ago
JSON representation
:bookmark: Better factor handling for R
- Host: GitHub
- URL: https://github.com/jonmcalder/refactor
- Owner: jonmcalder
- License: mit
- Created: 2016-08-29T23:08:07.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-11-13T15:11:32.000Z (over 5 years ago)
- Last Synced: 2024-06-13T00:52:05.143Z (almost 2 years ago)
- Topics: factor, package, r
- Language: R
- Homepage: https://jonmcalder.github.io/refactor/
- Size: 257 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
library(refactor, warn.conflicts = FALSE)
```
# refactor
[](https://www.repostatus.org/#abandoned)
[](https://travis-ci.org/jonmcalder/refactor)
[](https://codecov.io/gh/jonmcalder/refactor)
**refactor** is an R package which aims to provide better handling of factors. Though R does have a special data type for factors, it isn't always explicity catered for in commonly used R functions, which can lead to unexpected and undesirable outcomes (e.g. see this [Win-Vector blog post](http://www.win-vector.com/blog/2014/09/factors-are-not-first-class-citizens-in-r/)). This observation formed the inspiration for 're'-factor: which is essentially to 're'-visit functions likely to be used with factor data and where possible to wrap, extend or override them in order to better cater for factor data, or at the very least to provide warnings when the integrity of the data could be compromised by an operation.
## Installation
refactor is not available on CRAN but you can easily install the latest development version from github using `devtools`:
```r
# install.packages("devtools")
devtools::install_github("jonmcalder/refactor")
```
## Overview
Below are a few examples to illustrate some scenarios in which refactor can improve on base R's handling of factors.
Please see the vignette for more details: `vignette("refactor", package = "refactor")`
#### Better factor creation with `cfactor()`
- get warnings whenever provided factor levels don't fully match those present in the data
```{r}
string <- c("a", "b", "c")
factor(string, levels = c("b", "c", "d"))
cfactor(string, levels = c("b", "c", "d"))
```
- numerical sequences in strings are detected and utilized to obtain correct orderings for factor levels
```{r}
hard_to_detect <- c("EUR 21 - EUR 22", "EUR 100 - 101", "EUR 1 - EUR 10", "EUR 11 - EUR 20")
factor(hard_to_detect, ordered = TRUE)
cfactor(hard_to_detect, ordered = TRUE)
```
#### Extension of the generic `cut` method to handle (discrete) integer data
- generate 'natural' intervals for factors when using cut on integer vectors
- e.g. `[1,3], [4,6], [7,9]` as opposed to `(0.5, 3.5], (3.5, 6.5], (6.5, 9.5]`
- label factor levels more intuitively
- e.g. `1-3, 4-6, 7-9` as opposed to `[1,3], [4,6], [7,9], or (0,3], (3,6], (6-9]`
```{r}
x_int <- 1:9
cut.default(x_int, breaks = 3)
cut(x_int, breaks = 3)
```
#### Extension of the generic `cut` method to handle (non-numeric) ordered data
- `cut` usually requires numeric data, but refactor extends `cut` to handle other ordered data
```{r}
some_letters <- factor(c('d','e','f','a','b','c','g','h','i'), ordered = TRUE)
cut(some_letters, breaks = c('a','c','f','i'), include.lowest = TRUE, ordered_result = TRUE)
```
## Contributions
Suggestions and feedback are most welcome.
Feel free to [open an issue](https://github.com/jonmcalder/refactor/issues) if you want to request a feature or report a bug, and make a pull request if you can contribute.