Drew Steen
Drew Steen

Reputation: 16607

How to deal with the possibility of multiple list elements having the same names

I have just noticed that it is possible to construct lists in which multiple elements have the same name, for instance:

l <- list(a=1, a="a")

When element names are used to extract the list elements, the first element matching the name is returned: l$a returns 1 without so much as a warning.

I typically extract list elements by name. Now I'm worried that I will accidentally create lists with multiple elements having the same name (for instance by trying to merge lists by a common index), access the wrong list elements, and never know there was a problem.

Every time I use a list, I could test whether it has multiple elements with the same name:

length(unique(names(l)))==length(names(l))

...but this is cumbersome. Is there a better way to deal with this potential problem?

Upvotes: 5

Views: 2815

Answers (1)

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162321

Not that I'd recommend this at all, but here's a perhaps less cumbersome way to ensure that you don't extract an element from a list that contains repeated names:

## Define a method for `[[` that first checks the list x for repeated names
`[[.CC` <- function(x,i,j,...,exact=TRUE) {
    if(!length(unique(names(x))) == length(names(x))) {
        stop("List contains multiple elements with the same name")
    } else {
        NextMethod()
    }
}

## Write  a function that prepends the class that triggers the method above
CC <- function(X) {
    class(X) <- c("CC", class(X))
    X
}

## Try it out
l <- list(a=1, a="a")
m <- list(a=1, b="a")

CC(l)[["a"]]
# Error in `[[.CC`(CC(l), "a") : 
#   List contains multiple elements with the same name

CC(m)[["a"]]
# [1] 1

Upvotes: 3

Related Questions