Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pattern-matching / 'map_when' helper? #246

Closed
kevinushey opened this issue Aug 26, 2017 · 4 comments
Closed

pattern-matching / 'map_when' helper? #246

kevinushey opened this issue Aug 26, 2017 · 4 comments
Labels

Comments

@kevinushey
Copy link
Contributor

kevinushey commented Aug 26, 2017

This would be something similar to case_when(), but for mapping conditions to operations -- something like a generalized switch.

As an example, the following code:

mtcars %>% map_when(
  starts_with("d") ~ prod(.),
  contains("a")    ~ sum(.),
  c("mpg", "cyl")  ~ cor(.)
)

would evaluate to something like:

> mtcars %>% map_when(
+   starts_with("d") ~ prod(.),
+   contains("a")    ~ sum(.),
+   c("mpg", "cyl")  ~ cor(.)
+ )
[[1]]
[1] 1.218225e+91

[[2]]
[1] 336.09

[[3]]
          mpg       cyl
mpg  1.000000 -0.852162
cyl -0.852162  1.000000

Would this be worth adding to rlang (or somewhere similar)? Or does this already exist as some function I'm not aware of?

@kevinushey kevinushey changed the title generalized pattern matching (super switch)? pattern-matching / 'map_when' helper? Aug 26, 2017
@lionel-
Copy link
Member

lionel- commented Aug 26, 2017

That might fit in the vctrs package, which will probably be the home of case_when() in the future.

@lionel- lionel- added the vector label Mar 5, 2018
@lionel-
Copy link
Member

lionel- commented Sep 3, 2018

/move to vctrs

@lionel-
Copy link
Member

lionel- commented Sep 3, 2018

and nothing happens :p where are you move bot?

@ghost ghost deleted a comment from lionel- Sep 3, 2018
@ghost
Copy link

ghost commented Sep 3, 2018

This issue was moved by lionel- to r-lib/vctrs#66.

@ghost ghost closed this as completed Sep 3, 2018
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants