[Rd] indexing by character with identical names

Jake Michaelson jake.michaelson at gmail.com
Fri Sep 19 11:15:47 CEST 2008


Hi all,

I've been using R for a while, but was puzzled when I just barely
noticed this behavior:

> x <- 1:10
> names(x) <- c("a","b","a",letters[1:7])
> x
 a  b  a  a  b  c  d  e  f  g 
 1  2  3  4  5  6  7  8  9 10 

> x["a"]
a 
1 

... that is, if a vector has identical (and therefore ambiguous) names,
it only returns the first match when the vector is indexed by a
non-unique name.  From my perspective, a more sensible behavior would be
to return *all* elements which carry the non-unique name (e.g.
x[names(x)=="a"] ), since it's ambiguous as to which element the user
actually wanted to extract.  It just seems safer than quietly returning
only the first matched element with no warning or other indication as to
what's actually happening.

I couldn't find that this behavior is explicitly documented in the help
file for '[' or 'names'.

I also couldn't find any prior discussion of this behavior.  Is this
something that could be addressed in an upcoming release of R? 

Thanks,

jake

R version 2.7.2 (2008-08-25) 
x86_64-unknown-linux-gnu



More information about the R-devel mailing list