This discussion reminds me of another disanalogy in vector/list
double/single-bracket numeric/named indexing.

First, here's what happens when the item is present. The behavior is
consistent (or at least makes sense) across the different ways of getting
the value from vectors and lists:

# Named indexing of element in vector/list:
v[['a']]
# [1] 1
l[['a']]
# [1] 1

v['a']
# a
# 1
l['a']
# $a
# [1] 1

# Numeric indexing of element in vector/list
v[[1]]
# [1] 1
l[[1]]
# [1] 1

v[1]
# a
# 1
l[1]
# $a
# [1] 1




Now, here's what happens when the item isn't present:

# Numeric [[-indexing of nonexistent item throws error for both vectors
# and lists:
v[[3]]
# Error in v[[3]] : subscript out of bounds
l[[3]]
# Error in l[[3]] : subscript out of bounds

# But named [[-indexing of nonexistent item differs between vectors
# and lists:
v[['c']]
# Error in v[["c"]] : subscript out of bounds
l[['c']]
# NULL


# Numeric [-indexing of a nonexistent item different behavior between
# vectors and lists:
v[3]
# <NA>
#   NA
l[3]
# $<NA>
# NULL

# For named [-indexing of nonexistent items, behavior is the same as with
# numeric indexing:
v['c']
# <NA>
#   NA
l['c']
# $<NA>
# NULL


-Winston


On Tue, Feb 26, 2013 at 6:42 PM, Hervé Pagès <hpages@fhcrc.org> wrote:

> 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] 1
>>
>
> FWIW, 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@fhcrc.org
> Phone:  (206) 667-5791
> Fax:    (206) 667-1319
>
>
> ______________________________**________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/**listinfo/r-devel<https://stat.ethz.ch/mailman/listinfo/r-devel>
>

	[[alternative HTML version deleted]]

