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 > sessionInfo() R Under development (unstable) (2013-02-11 r61902) Platform: x86_64-w64-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_United Kingdom.1252 [2] LC_CTYPE=English_United Kingdom.1252 [3] LC_MONETARY=English_United Kingdom.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United Kingdom.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base -- Patrick Burns pburns at pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming')
On 26/02/2013 10:30, Patrick Burns wrote:> Is it on purpose that `[[` strips the > names when used on an atomic vector?Yes, and documented! It does when used on a list, so is consistent. ?"[["? can be used to select a single element _dropping_ ?names?, whereas ?"["? keeps them, e.g., in ?c(abc = 123)[1]?.> > > c(a=1, b=2)[1] > a > 1 > > c(a=1, b=2)[[1]] > [1] 1 > > > > sessionInfo() > R Under development (unstable) (2013-02-11 r61902) > Platform: x86_64-w64-mingw32/x64 (64-bit) > > locale: > [1] LC_COLLATE=English_United Kingdom.1252 > [2] LC_CTYPE=English_United Kingdom.1252 > [3] LC_MONETARY=English_United Kingdom.1252 > [4] LC_NUMERIC=C > [5] LC_TIME=English_United Kingdom.1252 > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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] 1Yes, 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
Hi Patrick, On 02/26/2013 02: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] 1FWIW, here are a couple of other interesting facts about this: (a) [[ is about twice faster than [ for me (64-bit Ubuntu) on a named atomic vector. (b) [[ raises an error if the subscript is out of bounds: > c(a=1, b=2)[3] <NA> NA > c(a=1, b=2)[[3]] Error in c(a = 1, b = 2)[[3]] : subscript out of bounds Can be useful in some contexts. Cheers, H.> > > > sessionInfo() > R Under development (unstable) (2013-02-11 r61902) > Platform: x86_64-w64-mingw32/x64 (64-bit) > > locale: > [1] LC_COLLATE=English_United Kingdom.1252 > [2] LC_CTYPE=English_United Kingdom.1252 > [3] LC_MONETARY=English_United Kingdom.1252 > [4] LC_NUMERIC=C > [5] LC_TIME=English_United Kingdom.1252 > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > >-- Herv? Pag?s Program in Computational Biology Division of Public Health Sciences Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N, M1-B514 P.O. Box 19024 Seattle, WA 98109-1024 E-mail: hpages at fhcrc.org Phone: (206) 667-5791 Fax: (206) 667-1319