Re: [R] matrix from list

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Mon, 28 Apr 2008 09:45:03 +0100 (BST)

On Mon, 28 Apr 2008, Kenn Konstabel wrote:

> On Sun, Apr 27, 2008 at 4:43 AM, Greg Snow <Greg.Snow@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

______________________________________________
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.
Received on Mon 28 Apr 2008 - 08:54:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 28 Apr 2008 - 09:30:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive