[R] writing sample values in to a file

Dieter Menne dieter.menne at menne-biomed.de
Tue Dec 14 16:53:56 CET 2010



chandu wrote:
> 
>   I am relatively new to R. I would like to know how can we write the
> realizations (for example generated through rnorm or runif) in to a data
> file. It would be very inefficient to first generate values and then write
> them in to file using "write" function. Instead, is there a way to
> generate 1 value at a time and append them in to the file.
> 

"It would be very inefficient". Sounds like you tried it.

n = 30000
system.time(for (i in 1:n){
    cat(rnorm(1),file="a.txt",append=TRUE)
 }
)
system.time(write.table(rnorm(n),file="b.txt"))
 )
  
 user  system elapsed 
   3.98    8.66   16.11 

> system.time(write.table(rnorm(n),file="b.txt"))
   user  system elapsed 
   0.59    0.01    0.61 

So the inefficient method is about 25 times faster.

Dieter



-- 
View this message in context: http://r.789695.n4.nabble.com/writing-sample-values-in-to-a-file-tp3086286p3087354.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list