Luca Cerone
2016-Jul-19 16:34 UTC
[R] Concatenate two lists replacing elements with the same name.
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
William Dunlap
2016-Jul-19 16:44 UTC
[R] Concatenate two lists replacing elements with the same name.
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]]
Gabor Grothendieck
2016-Jul-19 17:20 UTC
[R] Concatenate two lists replacing elements with the same name.
Try this: Reduce(modifyList, list(x, y, z)) On Tue, Jul 19, 2016 at 12:34 PM, 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.-- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Luca Cerone
2016-Jul-20 15:01 UTC
[R] Concatenate two lists replacing elements with the same name.
thanks a lot for the help! On Tue, Jul 19, 2016 at 7:20 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> Try this: > > Reduce(modifyList, list(x, y, z)) > > On Tue, Jul 19, 2016 at 12:34 PM, 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. > > > > -- > Statistics & Software Consulting > GKX Group, GKX Associates Inc. > tel: 1-877-GKX-GROUP > email: ggrothendieck at gmail.com