From murdoch at stats.uwo.ca  Tue Feb 19 18:23:50 2008
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Tue, 19 Feb 2008 12:23:50 -0500
Subject: [Rd] Incorrect paste() output (PR#10801)
In-Reply-To: <20080219145009.A3B95282EF48@mail.pubhealth.ku.dk>
References: <20080219145009.A3B95282EF48@mail.pubhealth.ku.dk>
Message-ID: <47BB10A6.2090008@stats.uwo.ca>

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



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