[R] Printing integers in R "as is"

Jan T. Kim jtk at cmp.uea.ac.uk
Thu Apr 14 15:02:12 CEST 2005


On Thu, Apr 14, 2005 at 02:32:33PM +0300, Firas Swidan wrote:

> I am using the following command to print to a file (I omitted the file
> details):
> 
> cat( paste( paste(orientation, start, end, names,"\n"), paste(start, end,
> "exon\n"), sep=""))
> 
> where "orientation" and "names" are character vectors and "start" and
> "end" are integer vectors.

For printing formatted output of this kind, you're generally much better
off using sprintf, as in

    cat(sprintf("%2s  %8d  %8d  %s\n", orientation, as.integer(start), as.integer(end), names));

or, if length(names) > 1, you might consider

    sprintf("%2s  %8d  %8d  %s\n", orientation, as.integer(start), as.integer(end), paste(names, collapse = ", "));

etc. This assumes that start and end are numeric vectors of length 1,
which seems sensible to me based on the context I can conclude from the
variable names, and I think that sprintf in R-devel, and R 2.1.0 in the
near future will cycle over longer vectors too.

> The problem is that R coerce the integer vectors to characters. In
> general, that works fine, but when one of the integer is 100000 (or has
> more 0's) then R prints it as 1e+05. This behavior causes a lot of
> trouble for the program reading R's output.
> This problem occur with paste, cat,
> and print (i.e. paste(100000)="1e+05" and so on).

Are you certain that start and end are integer vectors? If in doubt,
check typeof(start) -- the fact that the values are integer does not
necessarily mean that the type is integer.

Best regards, Jan
-- 
 +- Jan T. Kim -------------------------------------------------------+
 |    *NEW*    email: jtk at cmp.uea.ac.uk                               |
 |    *NEW*    WWW:   http://www.cmp.uea.ac.uk/people/jtk             |
 *-----=<  hierarchical systems are for files, not for humans  >=-----*




More information about the R-help mailing list