Thanks a lot Jim. I just switched back to R from SAS, so I might carry over the old style of creating tons of SAS macro variables... Another related question is : Suppose each element in my lst now is a numeric character: lst=list(a1=rnorm(4), a2=rnorm(4),a3=rnorm(4)) Still the number of elements is not fixed. I'm trying to run a system of equations (actually just separate regressions) like the following: fit1= lm(lst[[1]]~lst[[2]]+lst[[3]]) fit2= lm(lst[[2]]~lst[[1]]+lst[[3]]) fit3= lm(lst[[3]]~lst[[1]]+lst[[2]]) Is there any easy way to do the above for a list of non-fixed length? Thanks a lot.
Wayne (Yanwei) Zhang Ttatistical Áesearch CNA ________________________________ From: jim holtman [mailto:jholt...@gmail.com] Sent: Monday, June 15, 2009 11:14 AM To: Zhang,Yanwei Cc: r-help@r-project.org Subject: Re: [R] Create R object Here is the answer for the second part: > lst=list(a1=matrix(rnorm(4),2,2), a2=matrix(rnorm(4),2,2),a3=matrix(rnorm(4),2,2)) > l.dim <- sapply(lst, dim) > l.dim a1 a2 a3 [1,] 2 2 2 [2,] 2 2 2 > all(l.dim[1,1] == l.dim[1,]) && all(l.dim[2,1] == l.dim[2,]) [1] TRUE > For the first part, why don't you just leave it a list and access the list elements by names? If you want to create new objects, the 'for' is fine. On Mon, Jun 15, 2009 at 11:58 AM, Zhang,Yanwei <yanwei.zh...@cna.com> wrote: Dear R users, I have two simple questions here, and hope someone can help me on this. Thanks in advance. 1. I have a list object lst=list(a1=matrix(rnorm(4),2,2), a2=matrix(rnorm(4),2,2),a3=matrix(rnorm(4),2,2)). Here I only use three elements for illustration, and in fact the length of lst, n, is unknown in advance. I want to define an object for each element of this lst, and the objects have names like "object1", "object2",...,"objectn". I could use a loop like the following: for (i in 1:length(lst)){ assign(paste("object",i,sep=""),lst[[i]]) } I wonder if there's a way to avoid this loop? 2. I want to validate whether all of the "object"s have the same dimension, that is, whether dim(object1)[1]==dim(object2)[1]==dim(object3)[1]& dim(object1)[2]==dim(object2)[2]==dim(object3)[2] is true. How can I do that easily since the length of lst is not fixed? Thanks. Wayne (Yanwei) Zhang Ttatistical *esearch > CNA > > > E-MAIL CONFIDENTIALITY NOTICE: The contents of this e-mail message and any attachments are intended solely for the addressee(s) and may contain confidential and/or legally privileged information. If you are not the intended recipient of this message or if this message has been addressed to you in error, please immediately alert the sender by reply e-mail and then delete this message and any attachments. If you are not the intended recipient, you are notified that any use, dissemination, distribution, copying, or storage of this message or any attachment is strictly prohibited. [[alternative HTML version deleted]] ______________________________________________ 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? E-MAIL CONFIDENTIALITY NOTICE: The contents of this e-mail message and any attachments are intended solely for the addressee(s) and may contain confidential and/or legally privileged information. If you are not the intended recipient of this message or if this message has been addressed to you in error, please immediately alert the sender by reply e-mail and then delete this message and any attachments. If you are not the intended recipient, you are notified that any use, dissemination, distribution, copying, or storage of this message or any attachment is strictly prohibited. [[alternative HTML version deleted]]
______________________________________________ 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.