Hi, I have 2 questions: Question 1: I define 2 variables: "a", "b":> a<-rbinom(4,10,0.8)output: [1] 9 7 8 8> b<-rbinom(2,6,0.7)output: [1] 4 5 if I write:> write.table(a, file = "filename", etc. etc. .... )it save only the values of variable "a". There is a way to save in a .txt file the values "a" and "b" as consecutive data? (but I would use many variables..) ..like this: 9 7 8 8 4 5 Question 2: is possible save data as rows? (9 7 8 8 4 5) thank's Eiger -- View this message in context: http://www.nabble.com/save-txt-file-tp25531307p25531307.html Sent from the R help mailing list archive at Nabble.com.
Eiger wrote:> > Hi, I have 2 questions: > > > Question 1: > > I define 2 variables: "a", "b": > >> a<-rbinom(4,10,0.8) > output: > [1] 9 7 8 8 > >> b<-rbinom(2,6,0.7) > output: > [1] 4 5 > > if I write: >> write.table(a, file = "filename", etc. etc. .... ) > it save only the values of variable "a". > > There is a way to save in a .txt file the values "a" and "b" as > consecutive data? (but I would use many variables..) > ..like this: > > 9 > 7 > 8 > 8 > 4 > 5 > > Question 2: > is possible save data as rows? > (9 7 8 8 4 5) > > thank's > > Eiger > >Assuming you are dealing with vectors of numbers, you can form a "row" of data using the append function to stick b onto the end of a:> append( a, b )[1] 9 7 8 8 4 5 This can be dumped to a file using write.table: write.table( t( append(a,b) ), 'test.txt', row.names=F, col.names=F, append=T ) The transpose function t() is used because write.table() assumes a singleton vector is a column vector and you want row vectors. If you have two matrices instead of vectors, say: matA <- matrix( rep(a,4), nrow=4, byrow=T ) matB <- matrix( rep(b,4), nrow=4, byrow=T ) Then you would use the cbind() function instead of the append() function to form your rows and drop the transpose function: write.table( cbind(a,b), 'test.txt', row.names=F, col.names=F, append=T ) If you want to overwrite (i.e. start a new file) when you use write.table, then drop the append=T. Hope this helps! -Charlie ----- Charlie Sharpsteen Undergraduate Environmental Resources Engineering Humboldt State University -- View this message in context: http://www.nabble.com/save-txt-file-tp25531307p25531324.html Sent from the R help mailing list archive at Nabble.com.
any r manual helps here, and there are many easy ways to do it. ?cbind ?matrix ?data.frame If you need it in rows, matrix transposition helps, or add the byrow argument when using the "matrix" function (or by.row; I don't remember from the top of my head). ?dim could also do the job if you make one vector out of a and b. Best, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von Eiger Gesendet: Wednesday, September 23, 2009 5:37 PM An: r-help at r-project.org Betreff: [R] save txt file Hi, I have 2 questions: Question 1: I define 2 variables: "a", "b":> a<-rbinom(4,10,0.8)output: [1] 9 7 8 8> b<-rbinom(2,6,0.7)output: [1] 4 5 if I write:> write.table(a, file = "filename", etc. etc. .... )it save only the values of variable "a". There is a way to save in a .txt file the values "a" and "b" as consecutive data? (but I would use many variables..) ..like this: 9 7 8 8 4 5 Question 2: is possible save data as rows? (9 7 8 8 4 5) thank's Eiger -- View this message in context: http://www.nabble.com/save-txt-file-tp25531307p25531307.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org 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.
cls59 wrote:> > Hope this helps! > > -Charlie >Thanks!!! :) -- View this message in context: http://www.nabble.com/save-data-in-a-txt-file-tp25531307p25554140.html Sent from the R help mailing list archive at Nabble.com.
a<-rbinom(4,10,0.8) b<-rbinom(2,6,0.7) # See ?write.table for many options write.table(t(a), file="D:/myfile.txt") # t() to give row of data. write.table(t(b), file="D:/myfile.txt", append=TRUE) --- On Wed, 9/23/09, Eiger <c_cb at hotmail.it> wrote:> From: Eiger <c_cb at hotmail.it> > Subject: [R] save txt file > To: r-help at r-project.org > Received: Wednesday, September 23, 2009, 5:37 PM > > Hi, I have 2 questions: > > > Question 1: > > I define 2? variables: "a", "b": > > > a<-rbinom(4,10,0.8) > output: > [1] 9 7 8 8 > > > b<-rbinom(2,6,0.7) > output: > [1] 4 5 > > if I write: > > write.table(a, file = "filename", etc. etc. .... ) > it save only the values of variable "a". > > There is a way to save in a .txt file the values? "a" > and "b" as consecutive > data?? (but I would use many variables..) > ..like this: > > 9 > 7 > 8 > 8 > 4 > 5 > > Question 2: > is possible save data as rows?? > (9 7 8 8 4 5) > > thank's > > Eiger > > > -- > View this message in context: http://www.nabble.com/save-txt-file-tp25531307p25531307.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org > 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. >__________________________________________________________________ The new Internet Explorer? 8 - Faster, safer, easier. Optimized for Yahoo!