[Rd] double bracket stripping names
Duncan Murdoch
murdoch.duncan at gmail.com
Tue Feb 26 14:12:06 CET 2013
On 13-02-26 5:30 AM, Patrick Burns wrote:
> Is it on purpose that `[[` strips the
> names when used on an atomic vector?
>
> > c(a=1, b=2)[1]
> a
> 1
> > c(a=1, b=2)[[1]]
> [1] 1
Yes, as Brian said. And this makes sense: the names are a property of
the container, not a property of the contents. Using single brackets
creates a new container with a subset of the elements. Using double
brackets extracts an element.
The fact that there's no way to hold a number other than in a length one
container means that the results are both length one containers, but
conceptually there's a difference between subsetting and extracting.
Perhaps at some distant date in the future scalar numbers will be
possible, and then maybe c(a=1, b=2)[[1]] would give one.
Duncan Murdoch
More information about the R-devel
mailing list