Arnav Ghosh
2006-May-10 20:56 UTC
[R] help with writing output from two different arrays to two columns in an output file
Hi, I am very new to R and I have written the following block of code to generate a gamma distribution for variable x (which is an array) and a function "y" whose array values depend on the individual array values of "x". 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"). Thanks in advance, Arnav [[alternative HTML version deleted]]
Robert Citek
2006-May-10 21:25 UTC
[R] help with writing output from two different arrays to two columns in an output file
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