Dear List: I have the following simple program: x<- sample(site) VarGuilda1<- var(tapply(x,site,func1)) VarGuilda2<- var(tapply(x,site,func2)) VarGuilda3<- var(tapply(x,site,func3)) VarGuilda4<- var(tapply(x,site,func4)) VarGuilda5<- var(tapply(x,site,func5)) VarGuilda6<- var(tapply(x,site,func6)) VarGuilda7<- var(tapply(x,site,func7)) VarGuilda8<- var(tapply(x,site,func8)) VarGuilda9<- var(tapply(x,site,func9)) Var<-cbind(VarGuilda1,VarGuilda2,VarGuilda3,VarGuilda4,VarGuilda5,VarGuilda6,VarGuilda7,VarGuilda8,VarGuilda9) write(Var,file="LAU_Var_01.txt", ncol=9) Every time I want to repeat this I have to change the name of *.txt file manually. How can I automate this, so it could be done for all the *.txt files (1000) I have to generate. Thanks in advance, Rog??rio -- Rog??rio R. Silva MZUSP http://www.mz.usp.br Linux User #354364 Linux counter http://counter.li.org
> > Every time I want to repeat this I have to change the name of *.txt > file manually. How can I automate this, so it could be done for all the > *.txt files (1000) I have to generate.Use something like: for (i in 1:1000){ name = paste('foo_', i, '.txt', sep='') You can figure out how to apply that idea. You can also refer to a variable by its name in string form, like a = get(paste('varname', i, sep='')) paste is your friend! W
Rog??rio Rosa da Silva wrote:> Dear List: > > I have the following simple program: > > x<- sample(site) > VarGuilda1<- var(tapply(x,site,func1)) > VarGuilda2<- var(tapply(x,site,func2)) > VarGuilda3<- var(tapply(x,site,func3)) > VarGuilda4<- var(tapply(x,site,func4)) > VarGuilda5<- var(tapply(x,site,func5)) > VarGuilda6<- var(tapply(x,site,func6)) > VarGuilda7<- var(tapply(x,site,func7)) > VarGuilda8<- var(tapply(x,site,func8)) > VarGuilda9<- var(tapply(x,site,func9)) > Var<-cbind(VarGuilda1,VarGuilda2,VarGuilda3,VarGuilda4,VarGuilda5,VarGuilda6,VarGuilda7,VarGuilda8,VarGuilda9) > write(Var,file="LAU_Var_01.txt", ncol=9) > > Every time I want to repeat this I have to change the name of *.txt > file manually. How can I automate this, so it could be done for all the > *.txt files (1000) I have to generate. > > > Thanks in advance, Rog??rio >Use ?paste. for(i in 1:1000) { ... file <- paste("LAU_Var_", i, ".txt", sep = "") write(Var, file = file, ncol = 9) } or if you want something like "LAU_VAR_xxxx.txt", you can use ?sprintf: file <- "LAU_Var_%04d.txt" for(i in 1:1000) { ... write(Var, file = sprintf(file, i), ncol = 9) } --sundar
Others have pointed out `paste' for constructing the file names. What I'd like to suggest is `cleaning up' the code a bit. Assuming func1, ..., func9 all return a single number, so that each tapply() call returns a vector, you can try something like:> f1 = mean > f2 = median > f3 = function(x) (mean(x) + median(x)) / 2 > ## Put the functions into a list. > flist = sapply(1:3, function(i) get(paste("f", i, sep=""))) > x = rnorm(20) ## generate some data > g = rep(1:4, each=5) ## generate groups > res = apply(sapply(flist, function(f) tapply(x, g, f)), 2, var) > res[1] 0.4264803 0.5438598 0.4639551 (You obviously need to work out the details to make sure this fits the data structure you have.) This is sort of taking the `whole object' approach. You can take it one step further by using replicate() instead of explicit for loop to repeat the 1000 times... Andy> From: Rog??rio Rosa da Silva > > Dear List: > > I have the following simple program: > > x<- sample(site) > VarGuilda1<- var(tapply(x,site,func1)) > VarGuilda2<- var(tapply(x,site,func2)) > VarGuilda3<- var(tapply(x,site,func3)) > VarGuilda4<- var(tapply(x,site,func4)) > VarGuilda5<- var(tapply(x,site,func5)) > VarGuilda6<- var(tapply(x,site,func6)) > VarGuilda7<- var(tapply(x,site,func7)) > VarGuilda8<- var(tapply(x,site,func8)) > VarGuilda9<- var(tapply(x,site,func9)) > Var<-cbind(VarGuilda1,VarGuilda2,VarGuilda3,VarGuilda4,VarGuil > da5,VarGuilda6,VarGuilda7,VarGuilda8,VarGuilda9) > write(Var,file="LAU_Var_01.txt", ncol=9) > > Every time I want to repeat this I have to change the > name of *.txt > file manually. How can I automate this, so it could be done > for all the > *.txt files (1000) I have to generate. > > > Thanks in advance, Rog??rio > > -- > Rog??rio R. Silva > MZUSP http://www.mz.usp.br > Linux User #354364 > Linux counter http://counter.li.org > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >