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.]
1e5 is 100000. 5 stand for the amount of zeros you add in behind that number. For example, lets say I have 1e7. I would put 7 zeros behind 1 so it will become 10,000,000.
In order to eliminate the exponential notation of the integer, we can use the global setting using options() method, by setting the scipen argument, that is options(scipen = n). Scipen: A penalty to be applied when deciding to print numeric values in fixed or exponential notation.
Remove notation in the entire R session You can disable scientific notation in the entire R session by using the scipen option. Global options of your R workspace. Use options(scipen = n) to display numbers in scientific format or fixed. Positive values bias towards fixed and negative towards scientific notation.
(1) Right-click a cell where you want to remove scientific notation, and (2) choose Format Cells… 2. In the Format Cells window, (1) select the Number category, (2) set the number of decimal places to 0, and (3) click OK.
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>
Alternatively, you can use integers which don't get printed in scientific notation. You can specify that your number is an integer by putting an "L" behind it, or doing as.integer.
> paste("abc",100000L) [1] "abc 100000" > paste("abc",as.integer(1000000000)) [1] "abc 1000000000"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With