[Rd] Definition of [[

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Mon Mar 16 09:28:39 CET 2009


somewhat one the side,

    l = list(1)
   
    l[[2]]
    # error, index out of bounds

    l[2][[1]]
    # NULL

that is, we can't extract from l any element at an index exceeding the
list's length (if we could, it would have been NULL or some sort of
_NA_list), but we can extract a sublist at an index out of bounds, and
from that sublist extract the element (which is NULL, 'the _NA_list').

that's not necessarily wrong, but "the item at index i" (l[[i]]) is not
equivalent to "the item in the sublist at index i".

vQ



Thomas Lumley wrote:
> On Sun, 15 Mar 2009, Stavros Macrakis wrote:
>
>> The semantics of [ and [[ don't seem to be fully specified in the
>> Reference manual.  In particular, I can't find where the following
>> cases are covered:
>>
>>> cc <- c(1); ll <- list(1)
>>
>>> cc[3]
>> [1] NA
>> OK, RefMan says: If i is positive and exceeds length(x) then the
>> corresponding selection is NA.
>>
>>> dput(ll[3])
>> list(NULL)
>> ? i is positive and exceeds length(x); why isn't this list(NA)?
>
> I think some of these are because there are only NAs for character,
> logical, and the numeric types. There isn't an NA of list type.
>
> This one shouldn't be list(NA) - which NA would it use?  It should be
> some sort of list(_NA_list_) type, and list(NULL) is playing that role.
>
>
>>> ll[[3]]
>> Error in list(1)[[3]] : subscript out of bounds
>> ? Why does this return NA for an atomic vector, but give an error for
>> a generic vector?
>
> Again, because there isn't an NA of generic vector type.
>
>>> cc[[3]] <- 34; dput(cc)
>> c(1, NA, 34)
>> OK
>>
>> ll[[3]] <- 34; dput(ll)
>> list(1, NULL, 34)
>> Why is second element NULL, not NA?
>> And why is it OK to set an undefined ll[[3]], but not to get it?
>
> Same reason for NULL vs NA.  The fact that setting works may just be
> an inconsistency -- as you can see from previous discussions, R often
> does not effectively forbid code that shouldn't work -- or it may be
> bug-compatibility with some version of S or S-PLUS.



More information about the R-devel mailing list