[Rd] indexing by character with identical names
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Fri Sep 19 12:07:25 CEST 2008
2008/9/19 Jake Michaelson <jake.michaelson at gmail.com>
> ... 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.
Some people may think it sensible that indexing a vector with an
index of length N returns a vector of length N:
> x[c("a","b","a")]
a b a
1 2 1
> I also couldn't find any prior discussion of this behavior. Is this
> something that could be addressed in an upcoming release of R?
Changing this would probably (no, definitely!) break stuff. Breaking
stuff is a bad thing. Perhaps the upcoming R version Aleph-null
release would be the right place for it...
R is not like other programming languages, so your perspective of
sensibility of minimal use here. You will get bitten by these gotchas.
I bet everyone on this mailing list has done a X[1,] and got bitten by
not having drop=FALSE....
Ouch. Writing tests on edge cases is a good thing, since it catches
these problems (if you write your tests well!).
Barry
More information about the R-devel
mailing list