Giter VIP home page Giter VIP logo

refactor's Introduction

refactor

Project Status: Abandoned – Initial development has started, but there has not yet been a stable, usable release; the project has been abandoned and the author(s) do not intend on continuing development. Build Status codecov

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). 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:

# 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
string <- c("a", "b", "c")
factor(string, levels = c("b", "c", "d"))
#> [1] <NA> b    c   
#> Levels: b c d
cfactor(string, levels = c("b", "c", "d"))
#> Warning: the following levels were empty: 
#>  d
#> Warning: the following levels were removed: 
#>  a
#> [1] <NA> b    c   
#> Levels: b c d
  • numerical sequences in strings are detected and utilized to obtain correct orderings for factor levels
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)
#> [1] EUR 21 - EUR 22 EUR 100 - 101   EUR 1 - EUR 10  EUR 11 - EUR 20
#> 4 Levels: EUR 1 - EUR 10 < EUR 100 - 101 < ... < EUR 21 - EUR 22
cfactor(hard_to_detect, ordered = TRUE)
#> [1] EUR 21 - EUR 22 EUR 100 - 101   EUR 1 - EUR 10  EUR 11 - EUR 20
#> 4 Levels: EUR 1 - EUR 10 < EUR 11 - EUR 20 < ... < EUR 100 - 101

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]
x_int <- 1:9
cut.default(x_int, breaks = 3)
#> [1] (0.992,3.67] (0.992,3.67] (0.992,3.67] (3.67,6.33]  (3.67,6.33] 
#> [6] (3.67,6.33]  (6.33,9.01]  (6.33,9.01]  (6.33,9.01] 
#> Levels: (0.992,3.67] (3.67,6.33] (6.33,9.01]
cut(x_int, breaks = 3)
#> [1] 1-3 1-3 1-3 4-6 4-6 4-6 7-9 7-9 7-9
#> Levels: 1-3 4-6 7-9

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
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)
#> [1] d-f d-f d-f a-c a-c a-c g-i g-i g-i
#> Levels: a-c < d-f < g-i

Contributions

Suggestions and feedback are most welcome.

Feel free to open an issue if you want to request a feature or report a bug, and make a pull request if you can contribute.

refactor's People

Contributors

jonmcalder avatar lorenzwalthert avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

refactor's Issues

additional warnings at point of factor creation

The following behaviour of cfactor is undesirable:

Duplicate levels

cfactor(c("a", "b"), levels = c("a", "a", "b")) yields a warning message that duplicated factor levels are depreciated. However, they are created anyways. This should not happen. Instead, before factor is called, we should remove duplicates from levels and issue a warning, saying duplicate levels were removed.

Confounding around underlying values x, labels and levels

It is actually possible to do the following without getting a warning:
factor(letters, levels = letters, labels = sample(letters)). Here, we essentially map a value x_i to an arbitrary value y_i with y_i is an element of X = (x_1, ... x_n). For most situations, this is probably not what the user wanted. It should at least cause a warning.

bins with width of one

Problem

When breaks are specified to closely together, bins of width one with ugly labels result, i.e. from cut(sample(10), breaks = c(1, 4, 6, 8, 9, 10)), we get levels 1-4 5-6 7-8 9-9 10-10

Solution

  • Issue a warning when bins of width one are created.
  • change the label to the break value of the bin (e.g. to 10) instead of creating a standard label (e.g. 10-10).

extreme input values

  • e.g. cut(sample(2), breaks = 3) and
  • cut(1L, breaks = 2)
    return an internal error from cut.default which might not be immediately clear to the user.

breaks given as integers yield error

Problem

Because of the assertive test in cut.integer for breaks, something like cut(sample(10), breaks = c(1L, 3L, 10L) fails.

Solution

Use assert(check_class(breaks, "integer"), check_class(breaks, "numeric"))

`ordered_result` argument in cut.integer and cut.ordered

Currently, the two cut methods for integer and ordered inherit from the default method regarding whether or not the output should be an ordered factor. In the helpfile, it is stated that

Logical: should the result be an ordered factor?

This seems to be a strange behavior since by nature, we are dealing with an sequence of ordered values, otherwise cut could not have been applied.

Hence, I suggest to change the default to TRUE and document this in the help file under details.

Missing values in input create error in cut.integer

reprex::reprex_info()
#> Created by the reprex package v0.1.1.9000 on 2017-10-16

library(refactor); cut(c(sample(10), NA, NA), breaks = 3)
#> 
#> Attache Paket: 'refactor'
#> The following objects are masked from 'package:base':
#> 
#>     append, ifelse
#> Error in if (breaks > max(x) - min(x) + 1) {: Fehlender Wert, wo TRUE/FALSE nötig ist

right argument without effect in cut.integer for default breaks_mode and breaks as scalars

The breaks_mode is designated to resemble cut.default, however, the right argument remains without effect in cut.integer for default breaks_mode and breaks as scalars as shown below. First, look at cut.default:

cut.default(1:4, 3, right = T)
[1] (0.997,2] (0.997,2] (2,3] (3,4]
Levels: (0.997,2] (2,3] (3,4]

cut.default(1:4, 3, right = F)
[1] [0.997,2) [2,3) [3,4) [3,4)
Levels: [0.997,2) [2,3) [3,4)

The second element of the output is first part of the first bin with right = TRUE, then part of the second bin with right = FALSE. As you stated in the documentation, the implementation of cut.integer does not consider the right argument in this case.

cut(1:4, 3, right = F)
[1] 1-2 1-2 3 4
Levels: 1-2 3 4

cut(1:4, 3, right = T)
[1] 1-2 1-2 3 4
Levels: 1-2 3 4
Since we name the breaks_mode "default", cut.integer needs to reproduce the underlying output (not the labels obviously) of cut.default.

include.lowest with inconsistent default

Problem

Depending on how breaks are specified, lowest values are included or not.

  • cut(sample(10), breaks = 2) gives the levels 1-5 and 6-10 (because the default is overwritten internally on line 78, while
  • cut(sample(10), breaks = c(1, 5, 10)gives the levels 2-5 and 6-10.

Suggested solution

Probably due to the default in cut.default, you wanted to keep include.lowest = FALSE. I think it is a counter intuitive default, so let's do away with it and change it to TRUE and hence remove line 78.

relationship of the arguments right and balance.

Problem

If length(breaks) == 1, the option right remains without effect, whereas in the case of length(breaks) > 1, the option balance remains without effect but no message is issued.

Suggested solution

Since both options refer to more or less the same (if not exactly the same), we should only keep one.

uniform call. argument for warnings / errors

Not sure whether it makes more sense to display where the error / warning occurred or not. I guess in interactive usage, it is pointless, but if you wrap for example cfactor in another function, it might be helpful?

breaks given as numeric should be coerced to integer

Problem

cut.integer with breaks ofc(1, 3.4, 6, 7, 10) creates levels 1-3.4 4.4-6 7-10.

Solution

breaks need to be coerced to integers giving a warning if any decimal numbers were rounded and the resulting breaks should be displayed.

# making architecture and creation data of factor more accessible

It should be possible to quickly see which level is mapped to which integer and what labels where used when it was created. This could be achieved in two steps:

  • Append creation data to factor as an attribute: levels is already an attribute.
  • Create a function that easily displays the relationship of underlying values, labels and levels.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.