Code Monkey home page Code Monkey logo

elif's Introduction

elif

This is a small package that exposes a single function to make managing if-else logic in R more pleasing to look at.

Usage

library(elif)

x <- with(mtcars, elif(
  mpg < 20 -> "low",
  mpg < 25 -> "med", "hi"
))

This produces the following output:

> aggregate(mpg~x, mtcars, function(x) c(min=min(x), max=max(x)))

    x mpg.min mpg.max
1  hi    26.0    33.9
2 low    10.4    19.7
3 med    21.0    24.4

You can also provide a vector as the final argument. Omitting a final assignment expression will fill the vector with NAs where a predicate is not matched.

y <- with(mtcars, elif(
  mpg < 20 -> "low",
  mpg < 25 -> "med", cyl
))

> table(y)
y
  4 low med 
  6  18   8 

elif is also dplyr friendly and provides a function alias more aligned with the tidyverse naming convention: else_if, although the two are functionally identical:

library(dplyr)

mtcars <- mtcars %>%
mutate(
  mpg_char = else_if(
    mpg > 25 -> 'hi',
    mpg < 20 -> 'lo', 'med'
  ),
  cyl_char = else_if(
    cyl == 4 -> 'four-banger',
    cyl == 8 -> 'gas-guzzler', 'average'
  ))

> head(mtcars)
   mpg cyl disp  hp drat    wt  qsec vs am gear carb mpg_char    cyl_char
1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4      med     average
2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4      med     average
3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1      med four-banger
4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1      med     average
5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2       lo gas-guzzler
6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1       lo     average

elif's People

Contributors

gravesee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

josemfdez-econ

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.