Dear list, in the following loop im generating objects of type table. What I would like to do is to put all those objects together in a list (that i called cc).I did this but the result is not what i espect to get: cc=list() d=1 for (i in data) { cc=list(cc,assign(paste("n",d,sep=""),table(i,subsample$vD31NADD))) d=d+1} I know that this won't work properly: cc=list(cc,assign(paste("n",d,sep=""),table(i,subsample$vD31NADD))) but I dont know a way to add to my list the new objects generated at each step of the loop. Thanks for your attention! [[alternative HTML version deleted]]
Hello, It depends a bit on what 'data' is, but look at... cc <- list() d <- 1 data <- expression(d1, d2, d3) for(i in data) {cc[[paste(i)]] <- table(i, subsample$vD31NADD} if 'data' is a character vector or of numbers, you would just need for(i in data) {cc[[i]] <- table(i, subsample$vD31NADD} Anyways, you can use ' i ' both for table() and as an index for your list, cc[[i]], as long as ' i ' is in a reasonable format. Best regards, Josh On Fri, May 7, 2010 at 6:54 AM, n.vialma at libero.it <n.vialma at libero.it> wrote:> > Dear list, > in the following loop im generating objects of type table. What I would like to do is to put all those objects together in a list (that i called cc).I did this but the result is not what i espect to get: > > > > cc=list() > d=1 > for (i in data) { > cc=list(cc,assign(paste("n",d,sep=""),table(i,subsample$vD31NADD))) > d=d+1} > I know that this won't work properly: > > cc=list(cc,assign(paste("n",d,sep=""),table(i,subsample$vD31NADD))) > but I dont know a way to add to my list the new objects generated at each step of the loop. > Thanks for your attention! > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/
Dear list, in the following loop im generating objects of type table. What I would like to do is to put all those objects together in a list (that i called cc).I did this but the result is not what i espect to get: cc=list() d=1 for (i in data) { cc=list(cc,assign(paste("n",d,sep=""),table(i,subsample$vD31NADD))) d=d+1} I know that this won't work properly: cc=list(cc,assign(paste("n",d,sep=""),table(i,subsample$vD31NADD))) but I dont know a way to add to my list the new objects generated at each step of the loop.Another information that I can give you is that data is a list. Thanks for your attention! [[alternative HTML version deleted]]
Don't use the assign function (it is not even clear what you are trying to do with it). Here is a better approach based on your code:> cc <- list() > d <- 1 > for (i in data) {+ cc[[d]] <- table( ... + d <- d + 1 + } Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of n.vialma at libero.it > Sent: Friday, May 07, 2010 7:54 AM > To: r-help at r-project.org > Subject: [R] for loop > > > Dear list, > in the following loop im generating objects of type table. What I would > like to do is to put all those objects together in a list (that i > called cc).I did this but the result is not what i espect to get: > > > > cc=list() > d=1 > for (i in data) { > cc=list(cc,assign(paste("n",d,sep=""),table(i,subsample$vD31NADD))) > d=d+1} > I know that this won't work properly: > > cc=list(cc,assign(paste("n",d,sep=""),table(i,subsample$vD31NADD))) > but I dont know a way to add to my list the new objects generated at > each step of the loop. > Thanks for your attention! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.