[R] sample

Sundar Dorai-Raj sundar.dorai-raj at PDF.COM
Wed May 5 20:58:29 CEST 2004



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




More information about the R-help mailing list