Skip to content

Commit 2744e4d

Browse files
committed
review suggestions, update fcase doc as well
1 parent 42b46de commit 2744e4d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

man/fcase.Rd

Lines changed: 12 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,12 @@ fcase(
5459
x > 5L, 3L,
5560
default = 5L
5661
)
62+
63+
# fcase can be used for recursion, unlike fifelse
64+
gcd_dt <- function(x,y) {
65+
r <- x%%y;
66+
return(fcase(!r, y, r, gcd_dt(x, y))) # Recursive call must be in the last when-value pair
67+
}
68+
gcd_dt(10, 1)
5769
}
5870
\keyword{ data }

man/fifelse.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
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.
1818
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 recursiveness, use \code{fcase} instead.
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.
2020
}
2121
\value{
2222
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}.

0 commit comments

Comments
 (0)