Hello I've got a question about list indexing. If anyone can help or point in the direction of some documentation, that'd be great. I've checked the Introduction to R and R language definition. Say you have a list of vectors:> x_list(c("a","b","c"),c("a","b","c","d"),c("a","b"),c("a","b","c","d","e")) > x[[1]] [1] "a" "b" "c" [[2]] [1] "a" "b" "c" "d" [[3]] [1] "a" "b" [[4]] [1] "a" "b" "c" "d" "e" Indexing single list components is fine using [[i]] and elements within using [[i]][j] but what if you want to get some information, for instance vector length or attributes of the components, across (or should that be down) the top level components. You can do it by looping across 1:length(list):> store <- numeric(0) > for(components in 1:length(x)) store <- c(store,length(x[[components]])) > store[1] 3 4 2 5 I know it's only two lines of code. I guess I'm wondering whether there is something that does what I'd expect from length(x[[1:4]]) if [[i]] allowed multiple indices. Thanks, David Orme. -- --------------------------------- David Orme Ph.D. Student - Macroevolution and diversity. Department of Biology, Imperial College, Silwood Park, Ascot, Berkshire. UK. SL5 7PY Phone: (020) 759 42446 (wk.) (01344) 891539 (hm.) E-mail: d.orme at ic.ac.uk -------------- next part -------------- An HTML attachment was scrubbed... URL: https://stat.ethz.ch/pipermail/r-help/attachments/20020116/373f5b40/attachment.html
David Orme wrote:> > Hello > > I've got a question about list indexing. If anyone can help or point > in the direction of some documentation, that'd be great. I've checked > the Introduction to R and R language definition. > > Say you have a list of vectors: > > > > x_list(c("a","b","c"),c("a","b","c","d"),c("a","b"),c("a","b","c","d","e")) > > x > [[1]] > [1] "a" "b" "c" > > [[2]] > [1] "a" "b" "c" "d" > > [[3]] > [1] "a" "b" > > [[4]] > [1] "a" "b" "c" "d" "e" > Indexing single list components is fine using [[i]] and elements > within using [[i]][j] but what if you want to get some information, > for instance vector length or attributes of the components, across (or > should that be down) the top level components. You can do it by > looping across 1:length(list): > > > store <- numeric(0) > > for(components in 1:length(x)) store <- > c(store,length(x[[components]])) > > store > [1] 3 4 2 5 > I know it's only two lines of code. I guess I'm wondering whether > there is something that does what I'd expect from length(x[[1:4]]) if > [[i]] allowed multiple indices.sapply(components,length) -d> > Thanks, > David Orme.-- Mag. David Meyer Wiedner Hauptstrasse 8-10 Vienna University of Technology A-1040 Vienna/AUSTRIA Department for Tel.: (+431) 58801/10772 Statistics and Probability Theory mail: david.meyer at ci.tuwien.ac.at -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
David Orme wrote:> > Hello > > I've got a question about list indexing. If anyone can help or point > in the direction of some documentation, that'd be great. I've checked > the Introduction to R and R language definition. > > Say you have a list of vectors: > > > > x_list(c("a","b","c"),c("a","b","c","d"),c("a","b"),c("a","b","c","d","e")) > > x > [[1]] > [1] "a" "b" "c" > > [[2]] > [1] "a" "b" "c" "d" > > [[3]] > [1] "a" "b" > > [[4]] > [1] "a" "b" "c" "d" "e" > Indexing single list components is fine using [[i]] and elements > within using [[i]][j] but what if you want to get some information, > for instance vector length or attributes of the components, across (or > should that be down) the top level components. You can do it by > looping across 1:length(list): > > > store <- numeric(0) > > for(components in 1:length(x)) store <- > c(store,length(x[[components]])) > > store > [1] 3 4 2 5 > I know it's only two lines of code. I guess I'm wondering whether > there is something that does what I'd expect from length(x[[1:4]]) if > [[i]] allowed multiple indices.sorry: sapply(x,length) of course. -d> > Thanks, > David Orme.-- Mag. David Meyer Wiedner Hauptstrasse 8-10 Vienna University of Technology A-1040 Vienna/AUSTRIA Department for Tel.: (+431) 58801/10772 Statistics and Probability Theory mail: david.meyer at ci.tuwien.ac.at -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._