Hi, Try: ls <- vector("list",3)
for (i in 1:3) { ls[[i]] <- summary(lm(y[,i]~x[,i])) } names(ls) <- paste0("lm",1:3) A.K. On Wednesday, May 14, 2014 10:45 AM, yuanzhi <yuanzhi...@usherbrooke.ca> wrote: Hi, I know all what you said, but it seems that you don't understand my problem. I am sorry about my poor english level. I know that we can use a list to store complicated objects. e.g. x<-cbind(rnorm(10),rnorm(10),rnorm(10)) y<-cbind(rnorm(10),rnorm(10),rnorm(10)) lm1<-summary(lm(y[,1]~x[,1])) lm2<-summary(lm(y[,2]~x[,2])) lm3<-summary(lm(y[,3]~x[,3])) ls<-list(lm1=lm1,lm2=lm2,lm3=lm3) so the results of linear models are stored in the list "ls", and we can use ls[[1]] to cite the results of the first linear model. But I want to do this part lm1<-summary(lm(y[,1]~x[,1])) lm2<-summary(lm(y[,2]~x[,2])) lm3<-summary(lm(y[,3]~x[,3])) in a loop if I have much more than 3 linear models to do. How can define a list "ls"so that I can write the codes as for (i in 1:3) { ls[[i]]<-summary(lm(y[,i]~x[,i])) } Jeff Newmiller wrote > Please do the reading I recommended before posting again. My example was > numeric because your example was numeric. For more complicated data, a > list is a type of vector that can hold such objects, and you would know > this if you had read the intro document. > --------------------------------------------------------------------------- > Jeff Newmiller The ..... ..... Go > Live... > DCN:< > jdnewmil@.ca > > 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 May 13, 2014 7:22:08 PM PDT, yuanzhi < > yuanzhi.li@ > > wrote: >>Thank you for your reply. >> >>Yes, there is a problem according to you suggestion. >>What if the value are not numerical, e.g. I want to use the variable to >>store the results of linear regression. >>can I use >>myvec <- vector( "numeric", 10 ) >>for ( i in 1:10 ) { >> myvec[ i ] <- summary(lm(y~x)) # y and x are different values in each >>loop. >>} >>? >> >>you advice seems only to be available when the function left allocates >>a >>numerical value to the variable, what if the function return other type >>of >>objects? >> >> >> >> >>Jeff Newmiller wrote >>> What is wrong with >>> >>> myvec <- vector( "numeric", 10 ) >>> for ( i in 1:10 ) { >>> myvec[ i ] <- i >>> } >>> >>> ? >>> >>> If you are using assign, IMHO you are probably doing whatever you are >>> doing wrong. >>> >>> If you want named elements, give the vector names: >>> >>> names( myvec ) <- paste0( "t", 1:10 ) >>> >>> and you can refer to them >>> >>> myvec[ "t3" ] >>> >>> Go read the "Introduction to R" document again... particularly the >>> discussion of indexing. >>> >>--------------------------------------------------------------------------- >>> Jeff Newmiller The ..... ..... Go >>> Live... >>> DCN:< >> >>> jdnewmil@.ca >> >>> > 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 May 13, 2014 5:47:12 PM PDT, Yuanzhi Li < >> >>> Yuanzhi.Li@ >> >>> > wrote: >>>>Hi, everyone >>>> >>>>I want to create a series of variables (e.g. t1, t2..., t10) which >>>>could >>>>be used in loops. My idea is to use function "assign" >>>> >>>>for (i in 1:10) >>>>{ >>>> assign(paste("t",i,sep=""), FUN) # allocate the value from FUN to >>>>variable ti >>>>} >>>> >>>>But when I create a vector containing the names of these variables >>and >>>>want to use the variables according to the subscript, it doesn't >>works. >>>> >>>>t<-noquote(paste("t",1:10,sep="")) >>>>t[1] >>>>t1 >>>>it returns only the name of variable t1, but not the value allocated >>to >>>> >>>>t1 by FUN. So what should I do to realize this? >>>> >>>>Or is there any better way to do this? >>>> >>>>Can we define a series of variables which can be used according to >>the >>>>subscript like >>>>t<-f(t1, t2..., t10), >>>>then we have 10 variables which can be used directly? >>>>for(i in 1:10) >>>>{ >>>> t[i]<-FUN# with the fines variables we can directly assign the >>value >>>>of FUN to ti >>>>} >>>>These are just my thoughts, I don't know whether there are available >>R >>>>codes to realized it. I am looking forward any help from you. >>>> >>>>Thanks in advance! >>>> >>>>Yuanzhi >>>> >>>>______________________________________________ >>>> >> >>> R-help@ >> >>> 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. >>> >>> ______________________________________________ >> >>> R-help@ >> >>> 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. >> >> >> >> >> >>-- >>View this message in context: >>http://r.789695.n4.nabble.com/How-to-create-multi-variables-tp4690465p4690470.html >>Sent from the R help mailing list archive at Nabble.com. >> >>______________________________________________ >> > R-help@ > 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. > > ______________________________________________ > R-help@ > 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. -- View this message in context: http://r.789695.n4.nabble.com/How-to-create-multi-variables-tp4690465p4690537.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@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. ______________________________________________ R-help@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.