You're looking for the assign() function.
See the first example in the help page for assign()
Something like
assign( paste( j,'.cd',i,'es.wash',sep='') , 1 )
instead of
names.cd[i].es.wash <- 1
paste() assembles the name as a character string, and then assign()
assigns a value to a variable with that name.
-Don
At 8:41 AM -0800 11/9/09, agm. wrote:
Hello,
I'm trying to run a loop that will subset my data into specific sets by
regions and by race/ethnicity. I'm trying to do this fairly compactly, and
I cannot get this to work.
A "simple" version of the code that I am trying to run is:
names <- c("white", "black", "asian", "hispanic")
for(j in names){
for(i in 1:9){
names.cd[i].es.wash <- 1
es.cd[i].names.w <- names.cd[i].es.wash +1
} ; }
I want the loop to create these variables so that I would have for example:
"white.cd1.es.wash" through "white.cd9.wash" and then the same for black,
hispanic, and asian
(However, none of these variables have been created yet outside of the loop)
When I try to run this, I get the following error message:
Error: unexpected symbol in:
"for(i in 1:9){
names.cd[i].es.wash"
Any idea what I am doing wrong? Thanks in advance for your help! I am
still trying to get my hands around this programming syntax
--
View this message in context:
http://*old.nabble.com/Complicated-For-Loop-%28to-me%29-tp26269479p26269479.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.
--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062
______________________________________________
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.