Search code examples
rr-faq

How can I paste 100000 without it being shortened to 1e+05?


Question: How can I use paste without 100000 becoming 1e+05?

Sorry in advance if this question seems frivolous (but it has resulted in a bug in my code). I use R to call an external script, so when I say e.g. paste("abc",100000) I want it to output "abc 100000" and not "abc 1e+05".

Here's an example of what it looks like on my screen:

> paste("abc",100000)
[1] "abc 1e+05"
> paste("abc",100001)
[1] "abc 100001"

This results in the bizarre behaviour that my script works for the input "100001" but not "100000".

I realise I could create a script to convert numbers to strings however I like, but I feel I shouldn't do this if there is an internal way to do the same thing (I suspect there is some "method" I'm missing).

[If it helps, I'm on Ubuntu 12.04.1 LTS ("precise"), running R version 2.14.1 (2011-12-22) in a terminal.]


Solution

  • See ?options, particularly scipen:

    R> paste("abc", 100000)
    [1] "abc 1e+05"
    R> options("scipen"=10)    # set high penalty for scientific display
    R> paste("abc", 100000)
    [1] "abc 100000"
    R> 
    

    Alternatively, control formatting tightly the old-school way via sprintf():

    R> sprintf("%s %6d", "abc", 100000)
    [1] "abc 100000"
    R>