Code Monkey home page Code Monkey logo

controller's Introduction

controller

Lifecycle: experimental R-CMD-check Codecov test coverage

controller is a collection of functions for working with controlled vocabularies in R. It introduces the control() verb, which recodes values in a vector using a lookup table of preferred and variant terms (a thesaurus).

Installation

You can install the development version of controlled from GitHub using the remotes package:

remotes::install_github("joeroe/controller")

Example

A common data-tidying problem is standardising variant terms for the same concept. Imagine we have a dataset that uses a number of different names for shades of the same colour. As data analysts, we naturally want to recode the data to eliminate this messy creativity, for example using dplyr::recode():

library(dplyr, warn.conflicts = FALSE)
shades <- c("daffodil", "purple", "magenta", "azure", "navy", "violet")

recode(shades,
       daffodil = "yellow",
       purple = "purple",
       magenta = "pink",
       azure = "blue",
       navy = "blue",
       violet = "purple")
#> [1] "yellow" "purple" "pink"   "blue"   "blue"   "purple"

But recoding this way can be tedious, especially if there are a large number of terms. With control(), we can instead use a data frame containing a thesaurus to replace the values:

library(controller)
data("colour_thesaurus")

control(shades, colour_thesaurus)
#> Replaced values:
#> ℹ daffodil → yellow
#> ℹ azure → blue
#> ℹ navy → blue
#> ℹ violet → purple
#> Warning: Some values of `x` were not matched in `thesaurus`:
#> ✖ magenta
#> [1] "yellow"  "purple"  "magenta" "blue"    "blue"    "purple"

control() also supports fuzzy matching, removing the need to exhaustively list variants for common causes of differing terminology. For example, to perform a case insensitive match to the thesaurus:

shades <- toupper(shades)
control_ci(shades, colour_thesaurus)
#> Replaced values:
#> ℹ DAFFODIL → yellow
#> ℹ PURPLE → purple
#> ℹ AZURE → blue
#> ℹ NAVY → blue
#> ℹ VIOLET → purple
#> Warning: Some values of `x` were not matched in `thesaurus`:
#> ✖ MAGENTA
#> [1] "yellow"  "purple"  "MAGENTA" "blue"    "blue"    "purple"

controller's People

Contributors

joeroe avatar

Watchers

 avatar  avatar  avatar

controller's Issues

Validate thesaurus in control()

At least that it's a two column data frame, but probably worth trying to check for appropriate structure, e.g. warn if second column contains duplicates.

Respect NAs

Currently control() coerces <NA> to "NA" and tries to find it in the thesaurus. It should instead ensure NAs in x are always returned as NAs.

Maybe we should warn if x contains NAs, though?

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.