Duplicate list names in R
When you have duplicate names and you call a subset by name, only the first element is returned. In fact, [[
will only ever give you one element anyway, so let's look at [
instead.
l["B"]# $B# [1] 5
We can also see that trying c("B", "B")
as the subset won't even give us the right result because R goes back and gets the first B
again.
l[c("B", "B")]# $B# [1] 5## $B# [1] 5
One of the safest ways to retrieve all the B
elements is to use a logical subset of the names()
vector. This will give us the correct elements.
l[names(l) == "B"]# $B# [1] 5## $B# [1] 7
This is a great example of why duplicate names should be avoided.