[[ vs. .subset2

View: New views
4 Messages — Rating Filter:   Alert me  

[[ vs. .subset2

by Gabor Grothendieck :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In the code below test2() gives an error message:

   Error in .subset2(x, i, exact = exact) :
     attempt to select less than one element

even though test(), which is nearly the same, gives
the expected result.  BOD is a data set that comes
with R.   Is this a bug?


idx <- 2

# returns expected result
test <- function(pf = parent.frame()) .subset2(BOD, pf$idx)
test()

# gives error message !!!
test2 <- function(pf = parent.frame()) BOD[[pf$idx]]
test2()


I tried this on Windows Vista under:

> R.version.string
[1] "R version 2.7.1 RC (2008-06-16 r45949)"

and

> R.version.string
[1] "R version 2.8.0 Under development (unstable) (2008-06-28 r46012)"

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

Re: [[ vs. .subset2

by Spencer Graves :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear Gabor:

      Does the following answer your question:

 > tst <- list(a=1, b=2)
 > tst[numeric(0)]
list()
 > tst[[numeric(0)]]
Error in tst[[numeric(0)]] : attempt to select less than one element

      Spencer

Gabor Grothendieck wrote:

> In the code below test2() gives an error message:
>
>    Error in .subset2(x, i, exact = exact) :
>      attempt to select less than one element
>
> even though test(), which is nearly the same, gives
> the expected result.  BOD is a data set that comes
> with R.   Is this a bug?
>
>
> idx <- 2
>
> # returns expected result
> test <- function(pf = parent.frame()) .subset2(BOD, pf$idx)
> test()
>
> # gives error message !!!
> test2 <- function(pf = parent.frame()) BOD[[pf$idx]]
> test2()
>
>
> I tried this on Windows Vista under:
>
>  
>> R.version.string
>>    
> [1] "R version 2.7.1 RC (2008-06-16 r45949)"
>
> and
>
>  
>> R.version.string
>>    
> [1] "R version 2.8.0 Under development (unstable) (2008-06-28 r46012)"
>
> ______________________________________________
> R-devel@... mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

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

Re: [[ vs. .subset2

by Gabor Grothendieck :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Not really.  The point of this is that promises seem to be
screwing up in conjunction with [[ but in light of the fact
that .subset2 works it suggests that there might be a better
implementation of [[.

Also some insight into what is really driving this problem
so one can more easily recognize it in other places would
be helpful.

This a simplification of an actual problem I had in
a large program and it took some time to track this
one down.  Also, I am not entirely happy with the
workarounds which are to use .subset2, as shown,
or force(pf) since it seems more natural to me that
it should just work out of the box.

On Fri, Jul 4, 2008 at 8:42 PM, Spencer Graves <spencer.graves@...> wrote:

> Dear Gabor:
>     Does the following answer your question:
>> tst <- list(a=1, b=2)
>> tst[numeric(0)]
> list()
>> tst[[numeric(0)]]
> Error in tst[[numeric(0)]] : attempt to select less than one element
>
>     Spencer
>
> Gabor Grothendieck wrote:
>>
>> In the code below test2() gives an error message:
>>
>>   Error in .subset2(x, i, exact = exact) :
>>     attempt to select less than one element
>>
>> even though test(), which is nearly the same, gives
>> the expected result.  BOD is a data set that comes
>> with R.   Is this a bug?
>>
>>
>> idx <- 2
>>
>> # returns expected result
>> test <- function(pf = parent.frame()) .subset2(BOD, pf$idx)
>> test()
>>
>> # gives error message !!!
>> test2 <- function(pf = parent.frame()) BOD[[pf$idx]]
>> test2()
>>
>>
>> I tried this on Windows Vista under:
>>
>>
>>>
>>> R.version.string
>>>
>>
>> [1] "R version 2.7.1 RC (2008-06-16 r45949)"
>>
>> and
>>
>>
>>>
>>> R.version.string
>>>
>>
>> [1] "R version 2.8.0 Under development (unstable) (2008-06-28 r46012)"
>>
>> ______________________________________________
>> R-devel@... mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>

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

Re: [[ vs. .subset2

by Peter Dalgaard :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Gabor Grothendieck wrote:
> Not really.  The point of this is that promises seem to be
> screwing up in conjunction with [[ but in light of the fact
> that .subset2 works it suggests that there might be a better
> implementation of [[
Not really; .subset2() is a shortcut that bypasses method dispatch, so
not a drop-in replacement for "[[". You also have

 > test3 <- function(pf = parent.frame()) "[[.data.frame"(BOD, pf$idx)
 > test3()
[1]  8.3 10.3 19.0 16.0 15.6 19.8


Something appears not quite right, but an easy workaround is to force
the promise:

 > test4 <- function(pf = parent.frame()) {pf;BOD[[pf$idx]]}
 > test4()
[1]  8.3 10.3 19.0 16.0 15.6 19.8



--
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@...)              FAX: (+45) 35327907

______________________________________________
R-devel@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
LightInTheBox - Buy quality products at wholesale price