[R] help with writing output from two different arrays to two columns in an output file

Robert Citek rwcitek at alum.calberkeley.org
Wed May 10 23:25:42 CEST 2006


Hello Arnav,

On May 10, 2006, at 3:56 PM, Arnav Ghosh wrote:
> The code is as follows:
> n=1000
> x=rgamma(n,1.5,2)
> y=vector("numeric",n)
> for (i in 1:n){
> y[i]=(2937/50000*exp(-1/1000*x[i])/x[i])
> }
> now I want to know how I can use "write"/"write.data" or anything  
> similar to
> write the output from x[i] and y[i] to two different colums  
> simultaneously
> in an output file (say "output.txt").

One way would be to create a data frame.  I tend to think of  
data.frames like sheets in a spreadsheet or tables in a database.   
Once you have your data in a data.frame, then you could write to/read  
from a file.  For example, within R:

# create the data.frame
myDataFrame <- data.frame(x,y)
# write it to a file
write.table(myDataFrame, file = "output.txt")
# read it from a file into a new data.frame
newDataFrame <- read.table("output.txt")

BTW, one new resource I discovered was the wiki.  The page you may be  
interested in:

http://wiki.r-project.org/rwiki/doku.php?id=tips:data-io:import_table

Regards,
- Robert
http://www.cwelug.org/downloads
Help others get OpenSource software.  Distribute FLOSS
for Windows, Linux, *BSD, and MacOS X with BitTorrent




More information about the R-help mailing list