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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Arnav Ghosh
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]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: help with writing output from two different arrays to two columns in an output file

Robert Citek

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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: help with writing output from two different arrays to two columns in an output file

Arnav Ghosh
Hi Robert,
             That worked perfectly. Thanks a lot.
-Arnav

On 5/10/06, Robert Citek <[hidden email]> wrote:

>
>
> 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
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html