[R] Writing to a file with fixed precision
Dear R users; A follow-up question regarding writing to a file with fixed precision: Assuming for each column of x, I would like to have a different format, then I modify the code as: x.fmt - apply(x, 1, function(x) sprintf(%.14f %.10f %2.5f, x)) where three different formats are %.14f %.10f %2.5f. The error message I got is Error in sprintf(fmt, ...) : too few arguments. If put %1$.14f %2$.10f %3$2.5f. instead, a different error message becomes: Error in sprintf(fmt, ...) : reference to non-existent argument 2 I do not exactly know what the problem is, or is there a more elegant way of completing this task. Thank you Yihsu Chen The Johns Hopkins University On Mon, 10 Oct 2005, Marc Schwartz wrote: On Mon, 2005-10-10 at 19:50 -0400, Richard Hedger wrote: Hi, I'm trying to ouput to a filled with a fixed precision: eg. if I have data x=c(1.0,1.4,2.0), I want to be able to ouput the following to a file: 1.00 1.40 2.00 I was wondering if there was a function to do this in R? Thanks, Richard It is possible that someone has written such a function somewhere. It's called format(). x - c(1.0,1.4,2.0) write(format(x, nsmall=14)) does this. However, this is relatively easy using write.table(). You just need to pre-format the numeric values prior to writing to the file: write.table(sprintf(%.14f, x), data.txt, col.names = FALSE, row.names = FALSE, quote = FALSE) Using sprintf(), we force the floats to have 14 decimal places. sprintf() outputs character vectors, so we remove the quoting of the resultant character vectors and don't write column/row names. Note that if 'x' is a matrix, using sprintf() will return a vector. So you might want to use the following instead to retain the dims: x [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 x.fmt - apply(x, 1, function(x) sprintf(%.14f, x)) x.fmt [,1] [,2] [,3] [1,] 1.00 2.00 3.00 [2,] 4.00 5.00 6.00 [3,] 7.00 8.00 9.00 [4,] 10.00 11.00 12.00 write.table(x.fmt, data.txt, col.names = FALSE, row.names = FALSE, quote = FALSE) If needed, you can of course change the default delimiter from a to another character in write.table(). See ?write.table and ?sprintf. Yihsu Chen The Johns Hopkins University __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Writing to a file with fixed precision
On 5/18/2006 8:20 PM, YIHSU CHEN wrote: Dear R users; A follow-up question regarding writing to a file with fixed precision: Assuming for each column of x, I would like to have a different format, then I modify the code as: x.fmt - apply(x, 1, function(x) sprintf(%.14f %.10f %2.5f, x)) where three different formats are %.14f %.10f %2.5f. The error message I got is Error in sprintf(fmt, ...) : too few arguments. If put %1$.14f %2$.10f %3$2.5f. instead, a different error message becomes: Error in sprintf(fmt, ...) : reference to non-existent argument 2 I do not exactly know what the problem is, or is there a more elegant way of completing this task. The problem is that you are passing only one argument x to sprintf, so it doesn't like having 3 formats. sprintf will apply the same format 3 times to the 3 elements of x. I'm not sure of a clean way to get what you want, but you could do it by splitting x into 3 parts to pass to sprintf and using do.call, e.g. x.fmt - apply(x, 1, function(x) do.call(sprintf, c(%.14f %.10f %2.5f, as.list(x Duncan Murdoch Thank you Yihsu Chen The Johns Hopkins University On Mon, 10 Oct 2005, Marc Schwartz wrote: On Mon, 2005-10-10 at 19:50 -0400, Richard Hedger wrote: Hi, I'm trying to ouput to a filled with a fixed precision: eg. if I have data x=c(1.0,1.4,2.0), I want to be able to ouput the following to a file: 1.00 1.40 2.00 I was wondering if there was a function to do this in R? Thanks, Richard It is possible that someone has written such a function somewhere. It's called format(). x - c(1.0,1.4,2.0) write(format(x, nsmall=14)) does this. However, this is relatively easy using write.table(). You just need to pre-format the numeric values prior to writing to the file: write.table(sprintf(%.14f, x), data.txt, col.names = FALSE, row.names = FALSE, quote = FALSE) Using sprintf(), we force the floats to have 14 decimal places. sprintf() outputs character vectors, so we remove the quoting of the resultant character vectors and don't write column/row names. Note that if 'x' is a matrix, using sprintf() will return a vector. So you might want to use the following instead to retain the dims: x [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 x.fmt - apply(x, 1, function(x) sprintf(%.14f, x)) x.fmt [,1] [,2] [,3] [1,] 1.00 2.00 3.00 [2,] 4.00 5.00 6.00 [3,] 7.00 8.00 9.00 [4,] 10.00 11.00 12.00 write.table(x.fmt, data.txt, col.names = FALSE, row.names = FALSE, quote = FALSE) If needed, you can of course change the default delimiter from a to another character in write.table(). See ?write.table and ?sprintf. Yihsu Chen The Johns Hopkins University __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Writing to a file with fixed precision
On Mon, 10 Oct 2005, Marc Schwartz wrote: On Mon, 2005-10-10 at 19:50 -0400, Richard Hedger wrote: Hi, I'm trying to ouput to a filled with a fixed precision: eg. if I have data x=c(1.0,1.4,2.0), I want to be able to ouput the following to a file: 1.00 1.40 2.00 I was wondering if there was a function to do this in R? Thanks, Richard It is possible that someone has written such a function somewhere. It's called format(). x - c(1.0,1.4,2.0) write(format(x, nsmall=14)) does this. However, this is relatively easy using write.table(). You just need to pre-format the numeric values prior to writing to the file: write.table(sprintf(%.14f, x), data.txt, col.names = FALSE, row.names = FALSE, quote = FALSE) Using sprintf(), we force the floats to have 14 decimal places. sprintf() outputs character vectors, so we remove the quoting of the resultant character vectors and don't write column/row names. Note that if 'x' is a matrix, using sprintf() will return a vector. So you might want to use the following instead to retain the dims: x [,1] [,2] [,3] [,4] [1,]147 10 [2,]258 11 [3,]369 12 x.fmt - apply(x, 1, function(x) sprintf(%.14f, x)) x.fmt [,1][,2][,3] [1,] 1.00 2.00 3.00 [2,] 4.00 5.00 6.00 [3,] 7.00 8.00 9.00 [4,] 10.00 11.00 12.00 write.table(x.fmt, data.txt, col.names = FALSE, row.names = FALSE, quote = FALSE) If needed, you can of course change the default delimiter from a to another character in write.table(). See ?write.table and ?sprintf. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Writing to a file with fixed precision
On Tue, 2005-10-11 at 08:42 +0100, Prof Brian Ripley wrote: On Mon, 10 Oct 2005, Marc Schwartz wrote: On Mon, 2005-10-10 at 19:50 -0400, Richard Hedger wrote: Hi, I'm trying to ouput to a filled with a fixed precision: eg. if I have data x=c(1.0,1.4,2.0), I want to be able to ouput the following to a file: 1.00 1.40 2.00 I was wondering if there was a function to do this in R? Thanks, Richard It is possible that someone has written such a function somewhere. It's called format(). x - c(1.0,1.4,2.0) write(format(x, nsmall=14)) does this. Indeed. Sorry, I was not clear in my use of words. I was thinking along the lines of a single function call such as: write.fmt(x, file = data.txt, ndigits = 14) It would of course be easy enough to create such a wrapper using existing functions. I was aware of format(), but for some reason had in the back of my mind that the use of 'nsmall' was not consistent in the decimal place output based upon prior experience. The result of which led me to use the vectorized formatC() to control such output. I then shifted to using sprintf(), when in 2.1.0, it was vectorized. Using format() also adds the benefit of having methods for matrices, etc., as opposed to sprintf(). Thanks, Marc __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Writing to a file with fixed precision
Hi, I'm trying to ouput to a filled with a fixed precision: eg. if I have data x=c(1.0,1.4,2.0), I want to be able to ouput the following to a file: 1.00 1.40 2.00 I was wondering if there was a function to do this in R? Thanks, Richard Richard Hedger Département de Biologie Université Laval Québec, Canada, G1K 7P4 [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Writing to a file with fixed precision
On Mon, 2005-10-10 at 19:50 -0400, Richard Hedger wrote: Hi, I'm trying to ouput to a filled with a fixed precision: eg. if I have data x=c(1.0,1.4,2.0), I want to be able to ouput the following to a file: 1.00 1.40 2.00 I was wondering if there was a function to do this in R? Thanks, Richard It is possible that someone has written such a function somewhere. However, this is relatively easy using write.table(). You just need to pre-format the numeric values prior to writing to the file: write.table(sprintf(%.14f, x), data.txt, col.names = FALSE, row.names = FALSE, quote = FALSE) Using sprintf(), we force the floats to have 14 decimal places. sprintf() outputs character vectors, so we remove the quoting of the resultant character vectors and don't write column/row names. Note that if 'x' is a matrix, using sprintf() will return a vector. So you might want to use the following instead to retain the dims: x [,1] [,2] [,3] [,4] [1,]147 10 [2,]258 11 [3,]369 12 x.fmt - apply(x, 1, function(x) sprintf(%.14f, x)) x.fmt [,1][,2][,3] [1,] 1.00 2.00 3.00 [2,] 4.00 5.00 6.00 [3,] 7.00 8.00 9.00 [4,] 10.00 11.00 12.00 write.table(x.fmt, data.txt, col.names = FALSE, row.names = FALSE, quote = FALSE) If needed, you can of course change the default delimiter from a to another character in write.table(). See ?write.table and ?sprintf. HTH, Marc Schwartz __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html