[Rd] Incorrect paste() output (PR#10801)

Duncan Murdoch murdoch at stats.uwo.ca
Tue Feb 19 18:23:50 CET 2008


On 2/19/2008 9:50 AM, asfe at di.fc.ul.pt wrote:
> Hello,
> 
> I am writing this message because of an incorrect output by paste(). 
> Please try the following script to see if the evidence I collected is 
> reproducible:
> 
> x <- c(10152, 28177);
> y <- c(9576, 26625);
> d <- y - x;
> 
> d;
> [1]  -576 -1552
> 
> paste(d, collapse = ", ");
> [1] "-576, -1552"
> 
> x <- x / 1000;
> y <- y / 1000;
> d <- y - x;
> 
> d;
> [1] -0.576 -1.552
> 
> paste(d, collapse = ", ");
> [1] "-0.575999999999999, -1.552"
> 
> The output of the last paste() is incorrect. I am using R version 2.6.1.

It looks correct to me.  paste() doesn't use the same formatting rules 
as print() uses.  print() rounded the first number to 7 digits, but 
paste() uses as.character(), which gives

 > as.character(d)
[1] "-0.575999999999999" "-1.552"

If you want output formatted in a particular way, you should use sprintf 
or one of the format* functions.

Duncan Murdoch



More information about the R-devel mailing list