Skip to content
Prev 340115 / 398500 Next

How to create multi variables

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.li at 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

            

            
--
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 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.