-
Notifications
You must be signed in to change notification settings - Fork 1k
fcase / case_when function for data.table #4021
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
Changes from all commits
d1aece6
a23c887
e4deb09
f5c5b02
7c4bbe0
f8d0531
278142b
14dc062
490e197
9375930
ee033f0
ba5f88d
8b40897
7cfaebc
37c467c
4f1151e
02d9aac
a744fd9
db51a9a
3c3ea40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
\name{fcase} | ||
\alias{fcase} | ||
\title{fcase} | ||
\description{ | ||
\code{fcase} is a fast implementation of SQL \code{CASE WHEN} statement for R. Conceptually, \code{fcase} is a nested version of \code{\link{fifelse}} (with smarter implementation than manual nesting). It is comparable to \code{dplyr::case_when} and supports \code{bit64}'s \code{integer64} and \code{nanotime} classes. | ||
} | ||
\usage{ | ||
fcase(..., default=NA) | ||
} | ||
\arguments{ | ||
\item{...}{ A sequence consisting of logical condition (\code{when})-resulting value (\code{value}) \emph{pairs} in the following order \code{when1, value1, when2, value2, ..., whenN, valueN}. Logical conditions \code{when1, when2, ..., whenN} must all have the same length, type and attributes. Each \code{value} may either share length with \code{when} or be length 1. Please see Examples section for further details.} | ||
\item{default}{ Default return value, \code{NA} by default, for when all of the logical conditions \code{when1, when2, ..., whenN} are \code{FALSE} or missing for some entries. } | ||
} | ||
\value{ | ||
Vector with the same length as the logical conditions (\code{when}) in \code{...}, filled with the corresponding values (\code{value}) from \code{...}, or eventually \code{default}. Attributes of output values \code{value1, value2, ...valueN} in \code{...} are preserved. | ||
} | ||
\seealso{ | ||
\code{\link{fifelse}} | ||
} | ||
\examples{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would be helpful to have an example where the order of conditions matters There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure to understand. The order of conditions always matters (if I am not mistaken) |
||
x = 1:10 | ||
fcase( | ||
x < 5L, 1L, | ||
x > 5L, 3L | ||
) | ||
|
||
fcase( | ||
x < 5L, 1L:10L, | ||
x > 5L, 3L:12L | ||
) | ||
|
||
# Lazy evaluation example | ||
fcase( | ||
x < 5L, 1L, | ||
x >= 5L, 3L, | ||
x == 5L, stop("provided value is an unexpected one!") | ||
) | ||
|
||
# fcase preserves attributes, example with dates | ||
fcase( | ||
x < 5L, as.Date("2019-10-11"), | ||
x > 5L, as.Date("2019-10-14") | ||
) | ||
|
||
# fcase example with factor; note the matching levels | ||
fcase( | ||
x < 5L, factor("a", levels=letters[1:3]), | ||
x > 5L, factor("b", levels=letters[1:3]) | ||
) | ||
|
||
# Example of using the 'default' argument | ||
fcase( | ||
x < 5L, 1L, | ||
x > 5L, 3L, | ||
default = 5L | ||
) | ||
} | ||
\keyword{ data } |
Uh oh!
There was an error while loading. Please reload this page.