[Rd] Augment base::replace(x, list, value) to allow list= to be a predicate?
Serguei Sokol
@oko| @end|ng |rom |n@@-tou|ou@e@|r
Mon Mar 6 09:45:21 CET 2023
Le 04/03/2023 à 01:21, Pavel Krivitsky a écrit :
> Dear All,
>
> Currently, list= in base::replace(x, list, value) has to be an index
> vector. For me, at least, the most common use case is for list= to be
> some simple property of elements of x, e.g.,
>
> x <- c(1,2,NA,3)
> replace(x, is.na(x), 0)
>
> Particularly when using R pipes, which don't allow multiple
> substitutions,
Right, but anonymous function syntax can palliate to this:
x |> (\(x) replace(x, is.na(x), 0))()
> it would simplify many of such cases if list= could be a
> function that returns an index, e.g.,
>
> replace <- function (x, list, values, ...) {
> # Here, list() refers to the argument, not the built-in.
> if(is.function(list)) list <- list(x, ...)
> x[list] <- values
> x
> }
Before modifying the base of R, we should examine existing possibilities
to achieve the same goal.
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)
>
> Then, the following is possible:
>
> c(1,2,NA,3) |> replace(is.na, 0)
this becomes
c(1,2,NA,3) |> freplace(is.na, 0)
and looks quite acceptable for me.
Best,
Serguei.
>
> Any thoughts?
> Pavel
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
--
Serguei Sokol
Ingenieur de recherche INRAE
Cellule Mathématiques
TBI, INSA/INRAE UMR 792, INSA/CNRS UMR 5504
135 Avenue de Rangueil
31077 Toulouse Cedex 04
tel: +33 5 61 55 98 49
email: sokol using insa-toulouse.fr
http://www.toulouse-biotechnology-institute.fr/en/technology_platforms/mathematics-cell.html
More information about the R-devel
mailing list