Re: [Rd] double bracket stripping names

2013-02-26 Thread Winston Chang
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
l[3]
# $
# NULL

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


-Winston


On Tue, Feb 26, 2013 at 6:42 PM, Hervé Pagès  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
>
>> 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: hpa...@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
>

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] double bracket stripping names

2013-02-26 Thread Hervé Pagès

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

   > 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: hpa...@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


Re: [Rd] double bracket stripping names

2013-02-26 Thread Hadley Wickham
> 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.

I like to make  the distinction between simplifying and preserving
subsetting (maybe not the best names but the best I've been able to
come up with).  It's unfortunate (but understandable) that the syntax
is inconsistent between lists and matrices:  x[i] / x[[i]], vs x[i, ,
drop = F], x[i, ]

Hadley


-- 
Chief Scientist, RStudio
http://had.co.nz/

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] double bracket stripping names

2013-02-26 Thread Duncan Murdoch

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

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] double bracket stripping names

2013-02-26 Thread Prof Brian Ripley

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,  rip...@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, UKFax:  +44 1865 272595

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] double bracket stripping names

2013-02-26 Thread Patrick Burns

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
pbu...@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')

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel