Hi A toy example is probably the least ambiguous way of explaining what I'm trying to do.> library(ts) > zz<-list(a=rnorm(100),b=rt(100,3)) > zz.spec<-lapply(zz,spectrum,plot=F) > summary(zz.spec)Length Class Mode a 15 spec list b 15 spec list I'm looking for an elegant way to fetch components of the sub-lists a and b of zz.spec; e.g. to make a matrix with columns a$spec and b$spec. matrix(unlist(lapply(zz.spec,function(x){return(x$spec)})),ncol=length(zz.sp ec)) is about the best I've come up with. Is there a more direct way? This gets particularly important with nested sub-lists - lists of lists of lists. Is the best approach is to have the good sense to avoid such structures in the first place? Cheers Jason DISCLAIMER: This electronic message together with any attachments is confidential. If you are not the intended recipient, do not copy, disclose or use the contents in any way. Please also advise us by return e-mail that you have received the message and then please destroy. Carter Holt Harvey is not responsible for any changes made to this message and / or any attachments after sending by Carter Holt Harvey. We use virus scanning software but exclude all liability for viruses or anything similar in this email or any attachment. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi Jason, How about this: zz.spec<-lapply(zz,FUN=function(x){ spectrum(x,plot=F) }) yourmatrix<-cbind(zz.spec$a$spec,zz.spec$b$spec) Best, Jake On Wed, 10 Oct 2001, Turner, Jason wrote:> Hi > > A toy example is probably the least ambiguous way of explaining what I'm > trying to do. > > > library(ts) > > zz<-list(a=rnorm(100),b=rt(100,3)) > > zz.spec<-lapply(zz,spectrum,plot=F) > > summary(zz.spec) > Length Class Mode > a 15 spec list > b 15 spec list > > I'm looking for an elegant way to fetch components of the sub-lists a and b > of zz.spec; > e.g. to make a matrix with columns a$spec and b$spec. > > matrix(unlist(lapply(zz.spec,function(x){return(x$spec)})),ncol=length(zz.sp > ec)) > > is about the best I've come up with. Is there a more direct way? This gets > particularly > important with nested sub-lists - lists of lists of lists. Is the best > approach is to > have the good sense to avoid such structures in the first place? > > Cheers > > Jason > > DISCLAIMER: This electronic message together with any attachments is > confidential. If you are not the intended recipient, do not copy, disclose or > use the contents in any way. Please also advise us by return e-mail that you > have received the message and then please destroy. Carter Holt Harvey is not > responsible for any changes made to this message and / or any attachments after > sending by Carter Holt Harvey. We use virus scanning software but exclude all > liability for viruses or anything similar in this email or any attachment. > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thanks Jake, The example I gave is a toy one, however, and I neglected to mention that I need it automated because the number of members (in the example, a and b) isn't known in advance, and could typically be as high as 30. I got tired of typing it all out each time. Sorry I forgot the most crucial details - I keep forgetting that the whole world isn't working on this with me. ;-) Cheers Jason> Hi Jason, > > How about this: > > zz.spec<-lapply(zz,FUN=function(x){ > spectrum(x,plot=F) > }) > > yourmatrix<-cbind(zz.spec$a$spec,zz.spec$b$spec) > > Best, > Jake > > On Wed, 10 Oct 2001, Turner, Jason wrote: > > > Hi > > > > A toy example is probably the least ambiguous way of explaining what I'm > > trying to do. > > > > > library(ts) > > > zz<-list(a=rnorm(100),b=rt(100,3)) > > > zz.spec<-lapply(zz,spectrum,plot=F) > > > summary(zz.spec) > > Length Class Mode > > a 15 spec list > > b 15 spec list > > > > I'm looking for an elegant way to fetch components of the sub-lists a > and b > > of zz.spec; > > e.g. to make a matrix with columns a$spec and b$spec. > > > > > matrix(unlist(lapply(zz.spec,function(x){return(x$spec)})),ncol=length(zz. > sp > > ec)) > > > > is about the best I've come up with. Is there a more direct way? This > gets > > particularly > > important with nested sub-lists - lists of lists of lists. Is the best > > approach is to > > have the good sense to avoid such structures in the first place? > > > > Cheers > > > > Jason > > > >DISCLAIMER: This electronic message together with any attachments is confidential. If you are not the intended recipient, do not copy, disclose or use the contents in any way. Please also advise us by return e-mail that you have received the message and then please destroy. Carter Holt Harvey is not responsible for any changes made to this message and / or any attachments after sending by Carter Holt Harvey. We use virus scanning software but exclude all liability for viruses or anything similar in this email or any attachment. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"Turner, Jason" wrote:> > ...The example I gave is a toy one, however, and I neglected to mention that I > need it > automated because the number of members (in the example, a and b) isn't > known in > advance, and could typically be as high as 30. I got tired of typing it all > out each time. > Sorry I forgot the most crucial details - I keep forgetting that the whole > world isn't > working on this with me. ;-)> > > > > >... I'm looking for an elegant way to fetch components of the sub-lists a > > and bI don't find unpacking lists straightforward, and use recursion when I need a general solution. That is, to say, I test each component of a list, and if it is another list, I call the function with the sublist. At the bottom list, I apply the appropriate function and return the transformed data. For a toy example... # list2mat unpacks a list of vectors, possibly nested, and # returns a matrix whose columns are the vectors in the list. # Note that the vectors in the list must all be the same length. list2mat<-function(input.list) { firstname<-"" for(l in 1:length(input.list)) { if(is.list(input.list[[l]])) { if(exists("newmat",inherits=F)) { if(nchar(firstname)) { tempnames<-firstname firstname<-"" } else tempnames<-colnames(newmat) newmat<-cbind(newmat,list2mat(input.list[[l]])) } else { newmat<-list2mat(input.list[[l]]) firstname<-names(input.list[l]) } } else { if(exists("newmat",inherits=F)) { if(nchar(firstname)) { tempnames<-firstname firstname<-"" } else tempnames<-colnames(newmat) newmat<-cbind(newmat,input.list[[l]]) colnames(newmat)<-c(tempnames,names(input.list[l])) } else { newmat<-input.list[[l]] firstname<-names(input.list[l]) } } } return(newmat) }> testlist<-list(a=1:5,b=2:6,c=list(d=3:7,e=c("a","b","c","d","e"))) > list2mat(testlist)Jim -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._