[Rd] Augment base::replace(x, list, value) to allow list= to be a predicate?

Pavel Krivitsky p@kr|v|t@ky @end|ng |rom un@w@edu@@u
Tue Mar 7 11:49:41 CET 2023


Dear Serguei,

On Mon, 2023-03-06 at 09:45 +0100, Serguei Sokol wrote:
> Right, but anonymous function syntax can palliate to this:
> 
> x |> (\(x) replace(x, is.na(x), 0))()

This approach hardly makes for concise or readable code.

> 
> Before modifying the base of R, we should examine existing
> possibilities to achieve the same goal.

Backwards-compatible enhancements to existing functions in base R
appear in every major release. In this case, the proposed behaviour of
replace() would be consistent with that of the *apply() family of
functions.

> In this particular case and if the previous solution (anonymous 
> function) is not satisfactory a thin one-line wrapper can make the
> job:
> 
> freplace <- function (x, list, values, ...) replace(x, 
> if(is.function(list)) list <- list(x, ...) else list, values)
> 
> this becomes
> 
> c(1,2,NA,3) |> freplace(is.na, 0)
> 
> and looks quite acceptable for me.

One could always implement a function that has this functionality, of
course. In fact, one could write,

replace <- function (x, list, values, ...) base::replace(x, 
  if(is.function(list)) list <- list(x, ...) else list, values)

and not even have to change the name. However, having to write it every
time defeats the purpose of having it in base in the first place.

				Best Regards,
				Pavel

> 
> Best,
> Serguei.
> 
> > 
> >                         Any thoughts?
> >                         Pavel
> > ______________________________________________
> > R-devel using r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 



More information about the R-devel mailing list