This question already has answers here:
How can I disable scientific notation?
(4 answers)
Closed 6 years ago.
My plot is showing values on the y-axis in the form of e notation. Which command should I use to get the values in the numeric form. The values in the file used are in the numeric form?
Thanks
To set the use of scientific notation in your entire R session, you can use the scipen option. From the documentation (?options):
‘scipen’: integer. A penalty to be applied when deciding to print
numeric values in fixed or exponential notation. Positive
values bias towards fixed and negative towards scientific
notation: fixed notation will be preferred unless it is more
than ‘scipen’ digits wider.
So in essence this value determines how likely it is that scientific notation will be triggered. So to prevent scientific notation, simply use a large positive value like 999:
options(scipen=999)
Try format function:
> xx = 100000000000
> xx
[1] 1e+11
> format(xx, scientific=F)
[1] "100000000000"
Related
This question already has answers here:
How to format a number as percentage in R?
(10 answers)
Represent numeric value with typical dollar amount format
(4 answers)
Closed 5 years ago.
I want to know if it's possible to display values on a table as percentages but still be able to do arithmetic operations with them.
I have tried using percent() from scales package, but it seems like it transforms the values from numeric to character. I have tried to convert them back using as.numeric(), but it will not work either.
Any solutions? Thank you.
One very hacky way to do this would be as follows
x <- runif(10)
class(x) <- "percent"
print.percent <- function(x) print(scales::percent(as.numeric(x)))
This is probably useful for quick analyses or short scripts but I wouldn't put this into any kind of package or shared code.
This question already has answers here:
How to add leading zeros?
(8 answers)
Closed 6 years ago.
My problem is to create a sequence of IDs in a vector. The vector will contain 001 to 020 then 030 to 100.
I can generate numbers by
x <- c(1:20,30:100)
but this is not in the format I am interested.
x <- c(paste("00", 1:9, sep=""),paste("0", 10:99, sep=""),100)
As suggested by the Frank... Use sprinf for formatted output. I like the %f formatter to format numbers. It is designed to format floating point numbers. %f will be replaced by the number. You can add 0 in front of the f to get leading numbers. Or you can also define how many digits you want to have overall (in your case 3) and how many should be decimal (0 after the .). Play a little with it. It is great for formatted output, filename etc.
sprintf('%03.0f', c(1:20,30:100))
This question already has answers here:
Why are these numbers not equal?
(6 answers)
Closed 5 years ago.
When I to convert large numbers formatted as character strings to numeric R changes the last digits. This also happens when I pass it the number itself.
For example:
> options(scipen = 999)
> as.numeric("3411190080123000215")
[1] 3411190080123000320
> as.numeric(3411190080123000215)
[1] 3411190080123000320
This also happens when I use other numeric functions:
> floor(3411190080123000215)
[1] 3411190080123000320
Could this be an issue with my settings?
Thank you!!
The issue is that you are not actually using integers, you are using floats.
x <- as.numeric("3411190080123000215")
is.integer(x)
However, your number is too large to be stored as an integer anyway. Check out the gmp R package for arbitrarily large integers.
This question already has an answer here:
R floating point number precision being lost on coversion from character
(1 answer)
Closed 7 years ago.
How do I retain the full 16 digit precision when coercing a text to numeric in R?
My attempt below does not appear to do this...
x<-"0.501288104715059"
x<-as.double(x)
x
[1] 0.5012881
[Note this is similar to a previously asked question using as.numeric to convert a character to number but his question refers to the case of using as.double to convert a character to a number]
The code in fact does work - I just needed to set the number of digits to be displayed
x<-"0.501288104715059"
x<-as.double(x)
options(digits=16)
x
[1] 0.501288104715059
Might be useful to somebody else
This question already has answers here:
How to remove leading "0." in a numeric R variable
(5 answers)
Closed 9 years ago.
Writing an article with R + Sweave, I wish to hide the zero before decimal point for correlation results. For example, for a score of 0.85, I wish to show it as .85. Or if the score is negative, e.g. -0.85, it should be shown as -.85.
Right now I am doing it in a very ugly way, using substr(0.85, 2, 4) for positive values and paste("-", substr(-0.85, 3, 5), sep="") for negative values. I did some homework, checking functions such as format() and formatC in the base package, but none of them seemed to meet my needs.
So I wonder whether there is an easier way to hide the zero before decimal point in R? Any help is appreciated.
I'd use a regular expression:
sub('^(-)?0[.]', '\\1.', c(0.85, -0.85))
## [1] ".85" "-.85"