[R] Applying ifelse() on different functions

Duncan Murdoch murdoch.duncan at gmail.com
Sat Jul 30 22:41:05 CEST 2011


On 11-07-30 3:56 PM, Megh Dal wrote:
> Dear all, I am looking for some procedure to apply 'ifelse' condition on function. I have created an alternative to lapply() function with exactly same set of arguments named lapply1(), however with different internal codes.
>
> Therefore I want something like, if (some condition) then call lapply1() otherwise lapply() function. Ofcourse I can create a complete if()... else()... like of coding however wondering something may be better available because the arguments are all common.

Write it like this:

if (condition) f <- lapply1
else f <- lapply
f(args)

or even shorter:

f <- if (condition) lapply1 else lapply
f(args)

or shortest:

(if (condition) lapply1 else lapply)(args)

Duncan Murdoch



More information about the R-help mailing list