All Projects → RLesur → casewhen

RLesur / casewhen

Licence: MIT License
Create reusable dplyr::case_when() functions

Programming Languages

r
7636 projects

Projects that are alternatives of or similar to casewhen

Moderndive book
Statistical Inference via Data Science: A ModernDive into R and the Tidyverse
Stars: ✭ 527 (+723.44%)
Mutual labels:  dplyr, tidyverse, rstats
Dance
tibble() dancing 💃
Stars: ✭ 41 (-35.94%)
Mutual labels:  dplyr, rstats
Sparklyr
R interface for Apache Spark
Stars: ✭ 775 (+1110.94%)
Mutual labels:  dplyr, rstats
Tidyheatmap
Draw heatmap simply using a tidy data frame
Stars: ✭ 151 (+135.94%)
Mutual labels:  dplyr, tidyverse
Tidylog
Tidylog provides feedback about dplyr and tidyr operations. It provides wrapper functions for the most common functions, such as filter, mutate, select, and group_by, and provides detailed output for joins.
Stars: ✭ 428 (+568.75%)
Mutual labels:  dplyr, tidyverse
Tidyexplain
🤹‍♀ Animations of tidyverse verbs using R, the tidyverse, and gganimate
Stars: ✭ 558 (+771.88%)
Mutual labels:  dplyr, rstats
Tidyquery
Query R data frames with SQL
Stars: ✭ 138 (+115.63%)
Mutual labels:  dplyr, tidyverse
parcours-r
Valise pédagogique pour la formation à R
Stars: ✭ 25 (-60.94%)
Mutual labels:  dplyr, tidyverse
eeguana
A package for manipulating EEG data in R.
Stars: ✭ 16 (-75%)
Mutual labels:  dplyr, tidyverse
CSSS508
CSSS508: Introduction to R for Social Scientists
Stars: ✭ 28 (-56.25%)
Mutual labels:  dplyr, tidyverse
databases-w-r
Databases with R, the latest - rstudio::conf2019
Stars: ✭ 33 (-48.44%)
Mutual labels:  dbi, dbplyr
Timetk
A toolkit for working with time series in R
Stars: ✭ 371 (+479.69%)
Mutual labels:  dplyr, tidyverse
datar
A Grammar of Data Manipulation in python
Stars: ✭ 142 (+121.88%)
Mutual labels:  dplyr, tidyverse
Tidyquant
Bringing financial analysis to the tidyverse
Stars: ✭ 635 (+892.19%)
Mutual labels:  dplyr, tidyverse
Tidy
Tidy up your data with JavaScript, inspired by dplyr and the tidyverse
Stars: ✭ 307 (+379.69%)
Mutual labels:  dplyr, tidyverse
Sergeant
💂 Tools to Transform and Query Data with 'Apache' 'Drill'
Stars: ✭ 120 (+87.5%)
Mutual labels:  dplyr, rstats
Tidyversity
🎓 Tidy tools for academics
Stars: ✭ 155 (+142.19%)
Mutual labels:  tidyverse, rstats
Decryptr
An extensible API for breaking captchas
Stars: ✭ 154 (+140.63%)
Mutual labels:  tidyverse, rstats
R4ds Exercise Solutions
Exercise solutions to "R for Data Science"
Stars: ✭ 226 (+253.13%)
Mutual labels:  dplyr, tidyverse
implyr
SQL backend to dplyr for Impala
Stars: ✭ 74 (+15.63%)
Mutual labels:  dplyr, tidyverse

casewhen

Travis build status Coverage status lifecycle CRAN status

The goal of casewhen is to create reusable dplyr::case_when() functions.
SAS users may recognise a behavior close to the SAS FORMATS.

Installation

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("RLesur/casewhen")

Motivation

During data wrangling with dplyr, one may use several times identical case_when() clauses in different steps. This can lead to a non-DRY code.
This package provides a convenient mean to define and reuse dplyr::case_when() functions.

Examples

With casewhen, you can easily create reusable dplyr::case_when() functions with the function create_case_when():

library(dplyr)
library(casewhen)

people <- tribble(
  ~name, ~sex, ~seek,
  "Mary", "F", "M",
  "Henry", "M", "F"
)

cw_sex <- create_case_when(
  x == "F" ~ "Woman",
  x == "M" ~ "Man",
  TRUE ~ as.character(x),
  vars = "x"
)

people %>% 
  mutate(sex_label = cw_sex(sex), 
         seek_label = cw_sex(seek))
#> # A tibble: 2 x 5
#>   name  sex   seek  sex_label seek_label
#>   <chr> <chr> <chr> <chr>     <chr>     
#> 1 Mary  F     M     Woman     Man       
#> 2 Henry M     F     Man       Woman

Reusing a case_when() function is mainly convenient when the same transformation is performed across different datasets.

cw_sexyverse <- create_case_when(
  x == "F" | x == "female" & y == "Human" ~ "Woman",
  x == "M" | x == "male" & y == "Human" ~ "Man",
  TRUE ~ as.character(x),
  vars = c("x", "y")
)

people %>% 
  mutate(sex_label = cw_sexyverse(sex, "Human"))
#> # A tibble: 2 x 4
#>   name  sex   seek  sex_label
#>   <chr> <chr> <chr> <chr>    
#> 1 Mary  F     M     Woman    
#> 2 Henry M     F     Man

starwars %>%
  mutate(sex_label = cw_sexyverse(gender, species)) %>%
  select(name, gender, species, sex_label) %>%
  head()
#> # A tibble: 6 x 4
#>   name           gender species sex_label
#>   <chr>          <chr>  <chr>   <chr>    
#> 1 Luke Skywalker male   Human   Man      
#> 2 C-3PO          <NA>   Droid   <NA>     
#> 3 R2-D2          <NA>   Droid   <NA>     
#> 4 Darth Vader    male   Human   Man      
#> 5 Leia Organa    female Human   Woman    
#> 6 Owen Lars      male   Human   Man

dbplyr support

You only have to register the case_when functions to your connection:

con <- 
  DBI::dbConnect(RSQLite::SQLite(), ":memory:") %>%
  add_case_when(cw_sex, cw_sexyverse)

people_db <- copy_to(con, people)
starwars_db <- copy_to(con, starwars %>% select(name, gender, species))

people_db %>% 
  mutate(sex_label = cw_sex(sex), 
         seek_label = cw_sex(seek)) %>%
  show_query()

<SQL>

SELECT `name`, `sex`, `seek`, 
    CASE
      WHEN (`sex` = 'F') THEN ('Woman')
      WHEN (`sex` = 'M') THEN ('Man')
      WHEN (1) THEN (CAST(`sex` AS TEXT))
    END AS `sex_label`, 
    CASE
      WHEN (`seek` = 'F') THEN ('Woman')
      WHEN (`seek` = 'M') THEN ('Man')
      WHEN (1) THEN (CAST(`seek` AS TEXT))
    END AS `seek_label`
FROM `people`
people_db %>%
  mutate(sex_label = cw_sexyverse(sex, "Human")) %>%
  show_query()

<SQL>

SELECT `name`, `sex`, `seek`, 
    CASE
      WHEN (`sex` = 'F' OR `sex` = 'female' AND 'Human' = 'Human') THEN ('Woman')
      WHEN (`sex` = 'M' OR `sex` = 'male' AND 'Human' = 'Human') THEN ('Man')
      WHEN (1) THEN (CAST(`sex` AS TEXT))
    END AS `sex_label`
FROM `people`
starwars_db %>%
  mutate(sex_label = cw_sexyverse(gender, species)) %>%
  show_query()

<SQL>

SELECT `name`, `gender`, `species`, 
    CASE
      WHEN (`gender` = 'F' OR `gender` = 'female' AND `species` = 'Human') THEN ('Woman')
      WHEN (`gender` = 'M' OR `gender` = 'male' AND `species` = 'Human') THEN ('Man')
      WHEN (1) THEN (CAST(`gender` AS TEXT))
    END AS `sex_label`
FROM `starwars %>% select(name, gender, species)`
DBI::dbDisconnect(con)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].