[R] combining lists with same names with 'c'

Eric Archer eric.archer at noaa.gov
Thu Feb 15 11:59:58 CET 2007


I recently ran across an unexpected problem caused by combining lists 
that contained elements of the same name.  I naively thought that the 
elements would get overwritten, but rather the "new" element gets added 
with the same name. A simplified version is :

 > my.list <- list(a = 1)
 > new.list <- c(my.list, list(a = "a", b = "b"))
 > new.list
$a
[1] 1

$a
[1] "a"

$b
[1] "b"

 > new.list$a
[1] 1


I think I understand why this happens, but is there a way to do this 
kind of combining and force overwriting if the list already contains an 
equivalent name?  I can imagine a way using 'lapply', but I'm just 
checking to see if there is a way to do it with 'c'.  In the cases I'd 
be using this, I wouldn't necessarily know beforehand if the new list 
contained unique elements or was an "update" list.

Thanks!
Cheers,
e.


-- 

Eric Archer, Ph.D.
NOAA-SWFSC
8604 La Jolla Shores Dr.
La Jolla, CA 92037
858-546-7121,7003(FAX)
eric.archer at noaa.gov


"Lighthouses are more helpful than churches."
    - Benjamin Franklin

"...but I'll take a GPS over either one."
    - Craig George



More information about the R-help mailing list