Skip to content

Commit 8d1377e

Browse files
authored
Merge pull request #6151 from Rdatatable/fifdoc
Update fifelse documentation to reflect evaluation behavior
2 parents 07cfc30 + bda7b04 commit 8d1377e

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

man/fcase.Rd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
\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.}
1212
\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. }
1313
}
14+
\details{
15+
\code{fcase} evaluates each when-value pair in order, until it finds a \code{when} that is \code{TRUE}. It then returns the corresponding \code{value}. During evaluation, \code{value} will be evaluated regardless of whether the corresponding \code{when} is \code{TRUE} or not, which means recursive calls should be placed in the last when-value pair, see \code{Examples}.
16+
17+
\code{default} is always evaluated, regardless of whether it is returned or not.
18+
}
1419
\value{
1520
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.
1621
}
@@ -54,5 +59,13 @@ fcase(
5459
x > 5L, 3L,
5560
default = 5L
5661
)
62+
63+
# fcase can be used for recursion, unlike fifelse
64+
# Recursive function to calculate the Greatest Common Divisor
65+
gcd_dt = function(x,y) {
66+
r = x\%\%y
67+
fcase(!r, y, r, gcd_dt(x, y)) # Recursive call must be in the last when-value pair
68+
}
69+
gcd_dt(10L, 1L)
5770
}
5871
\keyword{ data }

man/fifelse.Rd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
}
1616
\details{
1717
In contrast to \code{\link[base]{ifelse}} attributes are copied from the first non-\code{NA} argument to the output. This is useful when returning \code{Date}, \code{factor} or other classes.
18+
19+
Unlike \code{\link[base]{ifelse}}, \code{fifelse} evaluates both \code{yes} and \code{no} arguments for type checking regardless of the result of \code{test}. This means that neither \code{yes} nor \code{no} should be recursive function calls. For recursion, use \code{fcase} instead.
1820
}
1921
\value{
2022
A vector of the same length as \code{test} and attributes as \code{yes}. Data values are taken from the values of \code{yes} and \code{no}, eventually \code{na}.
2123
}
2224
\seealso{
2325
\code{\link{fcoalesce}}
26+
27+
\code{\link{fcase}}
2428
}
2529
\examples{
2630
x = c(1:4, 3:2, 1:4)
@@ -37,5 +41,9 @@ fifelse(c(TRUE,FALSE,TRUE), yes, no)
3741
3842
# Example of using the 'na' argument
3943
fifelse(test = c(-5L:5L < 0L, NA), yes = 1L, no = 0L, na = 2L)
44+
45+
# Example showing both 'yes' and 'no' arguments are evaluated, unlike ifelse
46+
fifelse(1 == 1, print("yes"), print("no"))
47+
ifelse(1 == 1, print("yes"), print("no"))
4048
}
4149
\keyword{ data }

0 commit comments

Comments
 (0)