[R] Concatenate two lists replacing elements with the same name.

William Dunlap wdunlap at tibco.com
Tue Jul 19 18:44:28 CEST 2016


concatfun <- function(...) {
   Reduce(f=function(a,b){ a[names(b)] <- b ; a },x=list(...), init=list())
}


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Tue, Jul 19, 2016 at 9:34 AM, Luca Cerone <luca.cerone at gmail.com> wrote:

> Dear all,
> I would like to know if there is a function to concatenate two lists
> while replacing elements with the same name.
>
> For example:
>
> x <- list(a=1,b=2,c=3)
> y <- list( b=4, d=5)
> z <- list(a = 6, b = 8, e= 7)
>
> I am looking for a function "concatfun" so that
>
> u <- concatfun(x,y,z)
>
> returns:
>
> u$a=6
> u$b=8
> u$c=3
> u$d=5
> u$e=7
>
> I.e. it combines the 3 lists, but when names have the same value it
> keeps the most recent one.
>
> Does such a function exists?
>
> Thanks for the help,
>
> Cheers,
> Luca
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list