Dear friends - this is simple I know but I can figure it out without your help. I have for each of 2195 instances 10 variables measured at specific times from 6 to several hundred, so if I just take one of the instances, I can make a list of the 10 variables together with their variable times. But when I have 2195 such instances I cannot get it how to make a list of these individual lists As a toy example demonstrating mercilessly my problem, if ASL[j] is mean to take the list of here 5 entries made in RES[[i]] and I write this (ignoring the times) it certainly doesn't work ASL <- list() RES <- list() for (j in 1:5){ for (i in 1:5) ASL[[j]] <- RES[[i]] <- i^j } All best wishes Troels Ring Aalborg, Denmark
Is this what you mean: ASL <- list() for (j in 1:5){ RES <- list() for (i in 1:5) RES[[i]] <- i ^ j # create list ASL[[j]] <- RES # store 'list of list' } Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Mon, Feb 16, 2015 at 11:43 AM, Troels Ring <tring at gvdnet.dk> wrote:> Dear friends - this is simple I know but I can figure it out without your > help. > I have for each of 2195 instances 10 variables measured at specific times > from 6 to several hundred, so if I just take one of the instances, I can > make a list of the 10 variables together with their variable times. But > when I have 2195 such instances I cannot get it how to make a list of these > individual lists > > As a toy example demonstrating mercilessly my problem, if ASL[j] is mean > to take the list of here 5 entries made in RES[[i]] and I write this > (ignoring the times) it certainly doesn't work > ASL <- list() > RES <- list() > for (j in 1:5){ > for (i in 1:5) > ASL[[j]] <- > RES[[i]] <- i^j } > > All best wishes > Troels Ring > Aalborg, Denmark > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]
On 16 Feb 2015, at 17:43 , Troels Ring <tring at gvdnet.dk> wrote:> Dear friends - this is simple I know but I can figure it out without your help. > I have for each of 2195 instances 10 variables measured at specific times from 6 to several hundred, so if I just take one of the instances, I can make a list of the 10 variables together with their variable times. But when I have 2195 such instances I cannot get it how to make a list of these individual lists > > As a toy example demonstrating mercilessly my problem, if ASL[j] is mean to take the list of here 5 entries made in RES[[i]] and I write this (ignoring the times) it certainly doesn't work > ASL <- list() > RES <- list() > for (j in 1:5){ > for (i in 1:5) > ASL[[j]] <- > RES[[i]] <- i^j }Your description doesn't quite make sense to me, but if you really want ASL to be a list of lists, you want each member to be a list and the (i,j)th item accessed as ASL[[i]][[j]]. So I'd expect to see something like for (j .... { ASL[[j]] <- list() for (i .... ASL[[j]][[|]] <- .... } You also really don't want to start with an empty list and extend it on every iteration. If you need an n-element list, preallocate it using vector(n, mode="list"). If the above doesn't make sense, rephrase the question.... -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
You have two named objects when your goal is to have one that contains five others. ASL <- vector( "list", 5 ) for (j in 1:5){ ASL[[j]] <- vector( "list", 5 ) for (i in 1:5) { ASL[[j]][[i]] <- i^j } } --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On February 16, 2015 8:43:51 AM PST, Troels Ring <tring at gvdnet.dk> wrote:>Dear friends - this is simple I know but I can figure it out without >your help. >I have for each of 2195 instances 10 variables measured at specific >times from 6 to several hundred, so if I just take one of the >instances, >I can make a list of the 10 variables together with their variable >times. But when I have 2195 such instances I cannot get it how to make >a >list of these individual lists > >As a toy example demonstrating mercilessly my problem, if ASL[j] is >mean >to take the list of here 5 entries made in RES[[i]] and I write this >(ignoring the times) it certainly doesn't work >ASL <- list() >RES <- list() >for (j in 1:5){ >for (i in 1:5) >ASL[[j]] <- > RES[[i]] <- i^j } > >All best wishes >Troels Ring >Aalborg, Denmark > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.
Thanks to Jim, Peter and Jeff who all saw the solution! Best wishes Troels Den 16-02-2015 kl. 18:46 skrev Jeff Newmiller:> You have two named objects when your goal is to have one that contains five others. > > ASL <- vector( "list", 5 ) > for (j in 1:5){ > ASL[[j]] <- vector( "list", 5 ) > for (i in 1:5) { > ASL[[j]][[i]] <- i^j > } > } > > --------------------------------------------------------------------------- > Jeff Newmiller The ..... ..... Go Live... > DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... > Live: OO#.. Dead: OO#.. Playing > Research Engineer (Solar/Batteries O.O#. #.O#. with > /Software/Embedded Controllers) .OO#. .OO#. rocks...1k > --------------------------------------------------------------------------- > Sent from my phone. Please excuse my brevity. > > On February 16, 2015 8:43:51 AM PST, Troels Ring <tring at gvdnet.dk> wrote: >> Dear friends - this is simple I know but I can figure it out without >> your help. >> I have for each of 2195 instances 10 variables measured at specific >> times from 6 to several hundred, so if I just take one of the >> instances, >> I can make a list of the 10 variables together with their variable >> times. But when I have 2195 such instances I cannot get it how to make >> a >> list of these individual lists >> >> As a toy example demonstrating mercilessly my problem, if ASL[j] is >> mean >> to take the list of here 5 entries made in RES[[i]] and I write this >> (ignoring the times) it certainly doesn't work >> ASL <- list() >> RES <- list() >> for (j in 1:5){ >> for (i in 1:5) >> ASL[[j]] <- >> RES[[i]] <- i^j } >> >> All best wishes >> Troels Ring >> Aalborg, Denmark >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >
You can let lapply() do the preallocation and the looping for you with ASL <- lapply(1:5, function(j) lapply(1:5, function(i) i^j)) Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Feb 16, 2015 at 9:46 AM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:> You have two named objects when your goal is to have one that contains > five others. > > ASL <- vector( "list", 5 ) > for (j in 1:5){ > ASL[[j]] <- vector( "list", 5 ) > for (i in 1:5) { > ASL[[j]][[i]] <- i^j > } > } > > --------------------------------------------------------------------------- > Jeff Newmiller The ..... ..... Go Live... > DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live > Go... > Live: OO#.. Dead: OO#.. Playing > Research Engineer (Solar/Batteries O.O#. #.O#. with > /Software/Embedded Controllers) .OO#. .OO#. rocks...1k > --------------------------------------------------------------------------- > Sent from my phone. Please excuse my brevity. > > On February 16, 2015 8:43:51 AM PST, Troels Ring <tring at gvdnet.dk> wrote: > >Dear friends - this is simple I know but I can figure it out without > >your help. > >I have for each of 2195 instances 10 variables measured at specific > >times from 6 to several hundred, so if I just take one of the > >instances, > >I can make a list of the 10 variables together with their variable > >times. But when I have 2195 such instances I cannot get it how to make > >a > >list of these individual lists > > > >As a toy example demonstrating mercilessly my problem, if ASL[j] is > >mean > >to take the list of here 5 entries made in RES[[i]] and I write this > >(ignoring the times) it certainly doesn't work > >ASL <- list() > >RES <- list() > >for (j in 1:5){ > >for (i in 1:5) > >ASL[[j]] <- > > RES[[i]] <- i^j } > > > >All best wishes > >Troels Ring > >Aalborg, Denmark > > > >______________________________________________ > >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >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. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]