In R, when having a redundantly named vector, why is it not possible to retrieve all elements in the named vector with the selection operator?
v <- c(1,2,3,4,5)
names(v) <- c("a","b","c","c","a")
v["c"] ## Returns only 3, not c(3,4)
It looks like R assumes that vector names are unique and only retrieves the first element in a vector whose name match the argument in the selection operator.
Is this some kind of optimization? Would it not be beneficial if we were able to select multiple elements in a vector with the same name attribute?
Is the point to guarantee that the number of elements returned when using the indexing operator is the same as the number of elements in the indexing vector?
You don’t want to use names for what you’re trying to do. You’re making a categorical variable, not naming each item uniquely. This is an important semantic distinction.