[R] How to use ifelse without invoking warnings
Leonard Mada
|eo@m@d@ @end|ng |rom @yon|c@eu
Sat Oct 9 23:09:11 CEST 2021
Dear Ravi,
I have uploaded on GitHub a version which handles also constant values
instead of functions.
Regarding named arguments: this is actually handled automatically as well:
eval.by.formula((x > 5 & x %% 2) ~ (x <= 5) ~ ., FUN, y=2, x)
# [1] 1 4 9 16 25 6 14 8 18 10
eval.by.formula((x > 5 & x %% 2) ~ (x <= 5) ~ ., FUN, x=2, x)
# [1] 4 4 4 4 4 2 14 2 18 2
eval.by.formula((x > 5 & x %% 2) ~ (x <= 5) ~ ., list(FUN[[1]], 0, 1),
y=2, x)
# [1] 0 0 0 0 0 1 14 1 18 1
But it still needs proper testing and maybe optimization: it is possible
to run sapply on the filtered sequence (but I did not want to break
anything now).
Sincerely,
Leonard
On 10/9/2021 9:26 PM, Leonard Mada wrote:
> Dear Ravi,
>
>
> I wrote a small replacement for ifelse() which avoids such unnecessary
> evaluations (it bothered me a few times as well - so I decided to try
> a small replacement).
>
>
> ### Example:
> x = 1:10
> FUN = list();
> FUN[[1]] = function(x, y) x*y;
> FUN[[2]] = function(x, y) x^2;
> FUN[[3]] = function(x, y) x;
> # lets run multiple conditions
> # eval.by.formula(conditions, FUN.list, ... (arguments for FUN) );
> eval.by.formula((x > 5 & x %% 2) ~ (x <= 5) ~ ., FUN, x, x-1)
> # Example 2
> eval.by.formula((x > 5 & x %% 2) ~ (x <= 5) ~ ., FUN, 2, x)
>
>
> ### Disclaimer:
> - NOT properly tested;
>
>
> The code for the function is below. Maybe someone can experiment with
> the code and improve it further. There are a few issues / open
> questions, like:
>
> 1.) Best Name: eval.by.formula, ifelse.formula, ...?
>
> 2.) Named arguments: not yet;
>
> 3.) Fixed values inside FUN.list
>
> 4.) Format of expression for conditions:
>
> expression(cond1, cond2, cond3) vs cond1 ~ cond2 ~ cond3 ???
>
> 5.) Code efficiency
>
> - some tests on large data sets & optimizations are warranted;
>
>
> Sincerely,
>
>
> Leonard
>
> =======
>
> The latest code is on Github:
>
> https://github.com/discoleo/R/blob/master/Stat/Tools.Formulas.R
>
> [...]
>
More information about the R-help
mailing list