Hi , I know this should be easy, but I haven''t figured out a fast way to do it. I have a list of vectors: mylist <- list(vector1, vector2, ..., vectorN). (The vectors are made of integers. They vary in length from about 1 to 10). I also have a vector of indices: myidx <- c( i1, i2, ..., iN). From these I''d like to make this: newvector <- c(vector1[i1], vector2[i2], ..., vectorN[iN]). It''s easy to do this using a for loop, but I need something that will be fast for lists with lengths of about 100,000. I''ve thought about using lapply, but don''t see how I can use it with "myidx". Thanks in advance, Jeff Miller -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Jeff Miller <jmiller at xcaliber.com> writes:> Hi , > I know this should be easy, but I haven''t figured out a fast way to > do it. > I have a list of vectors: mylist <- list(vector1, vector2, ..., > vectorN). > (The vectors are made of integers. They vary in length from about 1 > to 10). > I also have a vector of indices: myidx <- c( i1, i2, ..., iN). > From these I''d like to make this: newvector <- c(vector1[i1], > vector2[i2], ..., vectorN[iN]). > It''s easy to do this using a for loop, but I need something that > will be fast for lists > with lengths of about 100,000. I''ve thought about using lapply, but > don''t see how I can use it with "myidx". > Thanks in advance, > Jeff MillerI don''t think it *is* easy. Generally, one gets stalled on these problems involving two or more parallel vectors. Occasionally the idea of having a "multiapply" function pops up, but nothing has transpired yet. The canonical way would be to use sapply(1:N,function(i)mylist[i][myidx[i]]) but that is not likely to be much faster than a for loop. If all the vectors were the same length, mylist could have been a matrix and matrix indexing (myarray[cbind(1:N,myidx)]) would work, but there''s nothing similar for a list of vectors of unequal length. So if you need the speed badly, I think you have to go to the C level. (A rather nicely sized little exercise for learning the .External interface, I would expect.) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /''_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>> Jeff Miller <jmiller at xcaliber.com> writes:>> It''s easy to do this using a for loop, but I need something that >> will be fast for lists >> with lengths of about 100,000. I''ve thought about using lapply, but >> don''t see how I can use it with "myidx". >> Thanks in advance, >> Jeff Miller> nothing similar for a list of vectors of unequal length. So if you > need the speed badly, I think you have to go to the C level. (A rather > nicely sized little exercise for learning the .External interface, I > would expect.)I also find that a bit of preprocessing of the data using perl is useful for large dataset manipulations, particularly if its something you need to do only at the start. Robert Denham Queensland Department of Natural Resources ************************************************************************ The information in this e-mail together with any attachments is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any form of review, disclosure, modification, distribution and/or publication of this e-mail message is prohibited. If you have received this message in error, you are asked to inform the sender as quickly as possible and delete this message and any copies of this message from your computer and/or your computer system network. ************************************************************************ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Sun, 17 Jun 2001 17:10:26 -0500, you wrote:> Hi , > I know this should be easy, but I haven''t figured out a fast way to >do it. > I have a list of vectors: mylist <- list(vector1, vector2, ..., >vectorN). > (The vectors are made of integers. They vary in length from about 1 >to 10). > I also have a vector of indices: myidx <- c( i1, i2, ..., iN). > From these I''d like to make this: newvector <- c(vector1[i1], >vector2[i2], ..., vectorN[iN]).I don''t know if this is fast or not, but I think it gives the result you want: unlist(sapply(myidx,function(x) mylist[[x]])) Duncan Murdoch -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._