An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090414/77e3da3b/attachment-0001.pl>
Controlling widths in write.fwf()
7 messages · Duncan Murdoch, Brendan Morse, Vemuri, Aparna
On 14/04/2009 7:28 PM, Vemuri, Aparna wrote:
Is there a way to handle the widths of values being written to a file using wrtite.fwf() ? For example, I used read.fwf(file, width.vector) to read a file. After making the necessary data manipulation, I want to write the data to a new file in the same width.vector format. Is there a way to specify this?
There is no write.fwf function, but you can use sprintf() to convert things to strings and writeLines to write those strings. There's a lot of flexibility in the formats allowed; see the man page. Duncan Murdoch
Hi Aparna, you could always use the write.table function and set sep="". This will put all of your data into a sort of "fixed-width" column depending on what you specify to separate the values. See basic example below: x<-matrix(nrow=2, ncol=2, c(1,2,3,4)) write.table(x, "/Users/morse07/Desktop/x.txt", sep="", col.names=FALSE, row.names=FALSE) Note that any spaces you insert between the "" after the sep command will insert 1 fixed-width column per space in your file. Hope that helps, Brendan
On Apr 14, 2009, at 7:28 PM, Vemuri, Aparna wrote:
Is there a way to handle the widths of values being written to a file using wrtite.fwf() ? For example, I used read.fwf(file, width.vector) to read a file. After making the necessary data manipulation, I want to write the data to a new file in the same width.vector format. Is there a way to specify this? Thanks in Advance Aparna [[alternative HTML version deleted]]
______________________________________________ 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.
Thanks Brendan. write.table() doesn't seem to do it for me. The problem is that my data is not formatted as well as you show in the example. There are numbers and strings of varying sizes and write.table() misses the format. Aparna -----Original Message----- From: Brendan Morse [mailto:morse.brendan at gmail.com] Sent: Tuesday, April 14, 2009 5:35 PM To: Vemuri, Aparna Cc: r-help at r-project.org Subject: Re: [R] Controlling widths in write.fwf() Hi Aparna, you could always use the write.table function and set sep="". This will put all of your data into a sort of "fixed-width" column depending on what you specify to separate the values. See basic example below: x<-matrix(nrow=2, ncol=2, c(1,2,3,4)) write.table(x, "/Users/morse07/Desktop/x.txt", sep="", col.names=FALSE, row.names=FALSE) Note that any spaces you insert between the "" after the sep command will insert 1 fixed-width column per space in your file. Hope that helps, Brendan
On Apr 14, 2009, at 7:28 PM, Vemuri, Aparna wrote:
Is there a way to handle the widths of values being written to a file using wrtite.fwf() ? For example, I used read.fwf(file, width.vector) to read a file. After making the necessary data manipulation, I want to write the data to a new file in the same width.vector format. Is there a way to specify this? Thanks in Advance Aparna [[alternative HTML version deleted]]
______________________________________________ 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.
Duncan I tried writeLines. But I need to print about 230000 lines and it is really slow. Thanks Aparna -----Original Message----- From: Duncan Murdoch [mailto:murdoch at stats.uwo.ca] Sent: Tuesday, April 14, 2009 4:34 PM To: Vemuri, Aparna Cc: r-help at r-project.org Subject: Re: [R] Controlling widths in write.fwf()
On 14/04/2009 7:28 PM, Vemuri, Aparna wrote:
Is there a way to handle the widths of values being written to a file using wrtite.fwf() ? For example, I used read.fwf(file, width.vector) to read a file. After making the necessary data manipulation, I want to write the data to a new file in the same width.vector format. Is there a way to specify this?
There is no write.fwf function, but you can use sprintf() to convert things to strings and writeLines to write those strings. There's a lot of flexibility in the formats allowed; see the man page. Duncan Murdoch
On 4/15/2009 11:45 AM, Vemuri, Aparna wrote:
Duncan I tried writeLines. But I need to print about 230000 lines and it is really slow.
This took about 1 second here: writeLines(as.character(1:230000), "C:/temp/test.txt") I can't see how to make it much faster than that. Duncan Murdoch
Thanks Aparna -----Original Message----- From: Duncan Murdoch [mailto:murdoch at stats.uwo.ca] Sent: Tuesday, April 14, 2009 4:34 PM To: Vemuri, Aparna Cc: r-help at r-project.org Subject: Re: [R] Controlling widths in write.fwf() On 14/04/2009 7:28 PM, Vemuri, Aparna wrote:
Is there a way to handle the widths of values being written to a file using wrtite.fwf() ? For example, I used read.fwf(file, width.vector) to read a file. After making the necessary data manipulation, I want to write the data to a new file in the same width.vector format. Is there a way to specify this?
There is no write.fwf function, but you can use sprintf() to convert things to strings and writeLines to write those strings. There's a lot of flexibility in the formats allowed; see the man page. Duncan Murdoch
______________________________________________ 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.
I was trying to format and write each line while looping over the entire matrix; which made it terribly slow. Without the loop, it works fine. Thanks Duncan. -----Original Message----- From: Duncan Murdoch [mailto:murdoch at stats.uwo.ca] Sent: Wednesday, April 15, 2009 10:04 AM To: Vemuri, Aparna Cc: r-help at r-project.org Subject: Re: [R] Controlling widths in write.fwf()
On 4/15/2009 11:45 AM, Vemuri, Aparna wrote:
Duncan I tried writeLines. But I need to print about 230000 lines and it is really slow.
This took about 1 second here: writeLines(as.character(1:230000), "C:/temp/test.txt") I can't see how to make it much faster than that. Duncan Murdoch
Thanks Aparna -----Original Message----- From: Duncan Murdoch [mailto:murdoch at stats.uwo.ca] Sent: Tuesday, April 14, 2009 4:34 PM To: Vemuri, Aparna Cc: r-help at r-project.org Subject: Re: [R] Controlling widths in write.fwf() On 14/04/2009 7:28 PM, Vemuri, Aparna wrote:
Is there a way to handle the widths of values being written to a file using wrtite.fwf() ? For example, I used read.fwf(file, width.vector) to read a file.
After
making the necessary data manipulation, I want to write the data to a new file in the same width.vector format. Is there a way to specify this?
There is no write.fwf function, but you can use sprintf() to convert things to strings and writeLines to write those strings. There's a
lot
of flexibility in the formats allowed; see the man page. Duncan Murdoch
______________________________________________ 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.