tplate at acm.org
2006-Jul-31 15:52 UTC
[Rd] bug in format.default: trim=TRUE does not always work as advertised (PR#9114)
DESCRIPTION OF PROBLEM: Output from format.default sometimes has whitespace around it when using big.mark="," and trim=TRUE. E.g.: > # works ok as long as big.mark is not actually used: > format(c(-1,1,10,999), big.mark=",", trim=TRUE) [1] "-1" "1" "10" "999" > # but if big.mark is used, output is justified and not trimmed: > format(c(-1,1,10,999,1e6), big.mark=",", trim=TRUE) [1] " -1" " 1" " 10" " 999" "1,000,000" > The documentation for the argument 'trim' to format.default() states: trim: logical; if 'FALSE', logical, numeric and complex values are right-justified to a common width: if 'TRUE' the leading blanks for justification are suppressed. Thus, the above behavior of including blanks for justification when trim=FALSE (in some situations) seems to contradict the documentation. PROPOSED FIX: The last call to prettyNum() in format.default() (src/library/base/R/format.R) has the argument preserve.width = "common" If this is changed to preserve.width = if (trim) "individual" else "common" then output is formatted correctly in the case above. A patch for this one line is attached to this message (patch done against the released R-2.3.1 source tarball (2006/06/01), the format.R file in this release is not different to the one in the current snapshot of the devel version of R). After making these changes, I ran "make check-all". I did not see any tests that seemed to break with these changes. -- Tony Plate