On Sun, Apr 27, 2008 at 4:43 AM, Greg Snow <[EMAIL PROTECTED]> 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. 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: [EMAIL PROTECTED] on behalf of Olivier Lefevre > Sent: Sat 4/26/2008 9:43 AM > To: [EMAIL PROTECTED] > 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@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@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@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.