[R] matrix from list
Prof Brian Ripley
ripley at stats.ox.ac.uk
Mon Apr 28 10:45:03 CEST 2008
On Mon, 28 Apr 2008, Kenn Konstabel wrote:
> On Sun, Apr 27, 2008 at 4:43 AM, Greg Snow <Greg.Snow at imail.org> wrote:
>
>> What if mylist <- list( 1:10, 101:110 , some.other.things) so the first 2
>> elements are vectors of length 10. then mylist[1:2] makes sense as still
>> being a list with the 2 vectors. What should mylist[[1:2]] return in this
>> case? One vector of length 20? or should it return a matrix with 2 columns
>> and 10 rows. Both of those make sense, how should the computer decide
>> between them (it may be obvious to you knowing the
>
> Note that the R language definition says,
>
> "for a list, the index can be a vector and each element of the vector is
> applied in turn to the list, the selected component, the selected component
> of that component, and so on. The result is still a single element. "
>
> So mylist[[1:2]] will return something in this case! -- the result is the
> second element of the first element of the list, or the same as
> mylist[[1]][[2]] or mylist[[1]][2].
>
> I don't find this particularly useful, and for better understanding, I would
> almost always prefer mylist[[1]][2] to mylist[[c(1,2)]] but there is a
> theoretical possibility that someone might find a use for this behavior. For
> these people, making list[[vector]] return something different may be
> disturbing.
Not theoretical -- it was an user wishlist request for compatibility with
S. And if the nesting is of variable depth, it is very useful.
This is also on the help page for [[, so there is little excuse for not
being aware of it.
>
> Kenn
>
>
>
>> context, but how can the computer decide). You can do either of these in
>> R by giving the computer a bit more information (as.matrix or unlist). What
>> if one of the vectors is character and one is numeric, what should the
>> return object be? What if the first element of mylist is the return object
>> from "lm" and the second element is a function, what should mylist[[1:2]]
>> return then?
>>
>> If you can come up with a set of rules that will cover every possible
>> case, then someone may be willing to implement those rules. But while it is
>> not obvious what to return without giving extra information, it is better to
>> require the extra information through other functions.
>>
>>
>>
>>
>>
>>
>> ________________________________
>>
>> From: r-help-bounces at r-project.org on behalf of Olivier Lefevre
>> Sent: Sat 4/26/2008 9:43 AM
>> To: r-help at stat.math.ethz.ch
>> Subject: Re: [R] matrix from list
>>
>>
>>
>> Olivier Lefevre wrote:
>>> Anyway you are right that it would still return the kind of object, only
>>> subsetted, which is not I want.
>>
>> I mean [] would do that; I know [[]] doesn't. Yet I still don't see why
>> one
>> accepts vector arguments but not the other: they are both indexing
>> operators. It is such inconsistencies that make languages hard to learn.
>>
>> -- O.L.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
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
More information about the R-help
mailing list