Calling ifelse
in nested calls is problematic for two main reasons:
It can be hard to read -- mapping the code to the expected output for such code can be a messy task/require a lot of mental bandwidth, especially for code that nests more than once
It is inefficient --
ifelse
can evaluate all of its arguments at both yes and no (see https://stackoverflow.com/q/16275149); this issue is exacerbated for nested calls
Details
Users can instead rely on a more readable alternative modeled after SQL
CASE WHEN statements, such as data.table::fcase
or dplyr::case_when
,
or use a look-up-and-merge approach (build a mapping table between values
and outputs and merge this to the input).
See also
linters for a complete list of linters available in lintr.