Dear R Users,
Is it possible to make file names dependent on a changing variable?
For instance. I generate random numbers in a loop and at each iteration I
want data to write to file (I do not want to write everything in one file
using 'append'):
for (i in 1:50){
x<-matrix(runif(100, min=0,max=1),nrow=5,ncol=20)
Write(t(x),file="Data_i.txt",ncolumns=5,sep="\t")
}
Of course file name Data_i.txt will be the same for changing i,
unfortunately.
Any suggestion would be appreciate
Robert
Use paste("Data_",i, sep="")
> From: Robert Mcfadden <robert-mcfadden at o2.pl>
> Date: Mon, 24 Jul 2006 11:06:38 +0200
> To: <r-help at stat.math.ethz.ch>
> Subject: [R] change the name of file
>
>
> Dear R Users,
> Is it possible to make file names dependent on a changing variable?
> For instance. I generate random numbers in a loop and at each iteration I
> want data to write to file (I do not want to write everything in one file
> using 'append'):
>
> for (i in 1:50){
> x<-matrix(runif(100, min=0,max=1),nrow=5,ncol=20)
> Write(t(x),file="Data_i.txt",ncolumns=5,sep="\t")
> }
>
> Of course file name Data_i.txt will be the same for changing i,
> unfortunately.
>
> Any suggestion would be appreciate
> Robert
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
Do you mean write.table instead of Write() ? Try
fn <- paste("Data_", i, ".txt", sep="")
write.table( t(x), file=fn, sep="\t" )
Regards, Adai
On Mon, 2006-07-24 at 11:06 +0200, Robert Mcfadden
wrote:> Dear R Users,
> Is it possible to make file names dependent on a changing variable?
> For instance. I generate random numbers in a loop and at each iteration I
> want data to write to file (I do not want to write everything in one file
> using 'append'):
>
> for (i in 1:50){
> x<-matrix(runif(100, min=0,max=1),nrow=5,ncol=20)
> Write(t(x),file="Data_i.txt",ncolumns=5,sep="\t")
> }
>
> Of course file name Data_i.txt will be the same for changing i,
> unfortunately.
>
> Any suggestion would be appreciate
> Robert
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
> Of course file name Data_i.txt will be the same for changing i, > unfortunately.?paste, e.g. paste("Data_", i, ".txt", sep="")
Great, little change and it works. Thank you.
Look
for (i in 1:50){
x<-matrix(runif(100, min=0,max=1),nrow=5,ncol=20)
write(t(x),file=paste("data_",i,".txt",sep=""),ncolumns=5,sep="\t")
}
> -----Original Message-----
> From: markleeds w verizon.net [mailto:markleeds w verizon.net]
> Sent: Monday, July 24, 2006 11:16 AM
> To: Robert Mcfadden
> Subject: Re: [R] change the name of file
>
> >From: Robert Mcfadden <robert-mcfadden w o2.pl>
> >Date: 2006/07/24 Mon AM 04:06:38 CDT
> >To: r-help w stat.math.ethz.ch
> >Subject: [R] change the name of file
>
> i think file=paste("data_", i,"txt",sep="")
> should work but someone else will probably
> have a better solution. if you try my idesa,
> check it because i haven't tested it.
>
>
>
> >
> >Dear R Users,
> >Is it possible to make file names dependent on a changing variable?
> >For instance. I generate random numbers in a loop and at each iteration
I
> >want data to write to file (I do not want to write everything in one
file
> >using 'append'):
> >
> >for (i in 1:50){
> >x<-matrix(runif(100, min=0,max=1),nrow=5,ncol=20)
> >Write(t(x),file="Data_i.txt",ncolumns=5,sep="\t")
> >}
> >
> >Of course file name Data_i.txt will be the same for changing i,
> >unfortunately.
> >
> >Any suggestion would be appreciate
> >Robert
> >
> >______________________________________________
> >R-help w stat.math.ethz.ch 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.
>
Dear Robert,
maybe (!) the following might do the job:
for (i in 1:50){
x<-matrix(runif(100, min=0,max=1),nrow=5,ncol=20)
Write(t(x),file=paste("Data_" i ".txt",
sep=""),ncolumns=5,sep="\t")
}
Hope it really does...
Thilo
On Monday 24 July 2006 11:06, Robert Mcfadden wrote:> Dear R Users,
> Is it possible to make file names dependent on a changing variable?
> For instance. I generate random numbers in a loop and at each iteration I
> want data to write to file (I do not want to write everything in one file
> using 'append'):
>
> for (i in 1:50){
> x<-matrix(runif(100, min=0,max=1),nrow=5,ncol=20)
> Write(t(x),file="Data_i.txt",ncolumns=5,sep="\t")
> }
>
> Of course file name Data_i.txt will be the same for changing i,
> unfortunately.
>
> Any suggestion would be appreciate
> Robert
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
--
________________________
Thilo Kellermann
Department of Psychiatry und Psychotherapy
RWTH Aachen University
Pauwelstr. 30
52074 Aachen
Tel.: +49 (0)241 / 8089977
Fax.: +49 (0)241 / 8082401
E-Mail: tkellermann at ukaachen.de