Format numbers to significant figures nicely in R
Format numbers to significant figures nicely in R
I want to format numbers in my reports to significant digits, but keep trailing significant zeroes and correctly format large numbers
For instance the numbers c(10.00001,12345,1234.5,123.45,1.2345,0.12345) to 3 significant digits should be 10.0, 12300, 1230, 123, 1.23, 0.123 but I get differing results with different methods (and none seem to work universaly.
> numbers<-c(10.00001,12345,1234.5,123.45,1.2345,0.12345) > for(n in seq(numbers)){ + print(signif(numbers[n],digits=3)) + print(format(numbers[n],digits=3)) + print(formatC(numbers[n], digits=3,format="fg")) + print(formatC(numbers[n], digits=3,format="fg", flag="#")) + } [1] 10 [1] "10" [1] " 10" [1] "10.0" [1] 12300 [1] "12345" [1] "12345" [1] "12345." [1] 1230 [1] "1234" [1] "1234" [1] "1234." [1] 123 [1] "123" [1] " 123" [1] "123." [1] 12.3 [1] "12.3" [1] "12.3" [1] "12.3" [1] 1.23 [1] "1.23" [1] "1.23" [1] "1.23" [1] 0.123 [1] "0.123" [1] "0.123" [1] "0.123"
Here, signif and format round the 10.00001 result. formatC with flag="#" correctly does the small numbers but not the large numbers.
Is there a better way ?
Answer by Dirk Eddelbuettel for Format numbers to significant figures nicely in R
Are you aware of prettyNum()
and all its options?
Answer by Richard Herron for Format numbers to significant figures nicely in R
A more barebones option is options()
, which just does rounding. If you plan on doing this a lot, I suggest checking out Sweave.
> a <- 1.23456789 > options(digits=2) > a [1] 1.2 > options(digits=6) > a [1] 1.23457
Answer by PaulHurleyuk for Format numbers to significant figures nicely in R
Sorry I never updated this at the time. None of the statements in my question, or prettynum worked. In the end I used
print(formatC(signif(numbers[n],digits=3), digits=3,format="fg", flag="#"))
which correctly coped with trailing zero's and big numbers.
Answer by Ben Haley for Format numbers to significant figures nicely in R
If you like scientific notation
> format(2^31-1, scientific = TRUE, digits = 3) [1] "2.15e+09"
Answer by hackR for Format numbers to significant figures nicely in R
Paul Hurley's method above worked well for me for both positive and negative numbers. Below is some code which modifies Paul's solution into a function in which the desired significant figures can be specified.
sigfig <- function(vec, n=3){ ### function to round values to N significant digits # input: vec vector of numeric # n integer is the required sigfig # output: outvec vector of numeric rounded to N sigfig formatC(signif(vec,digits=n), digits=n,format="fg", flag="#") } # end of function sigfig
to verify it works OK
numbers <- c(50000.01, 1000.001, 10.00001, 12345, 1234.5, 123.45, 1.2345, 0.12345, 0.0000123456, -50000.01, -1000.001,-10.00001, -12345, -1234.5, -123.45, -1.2345, -0.12345, -0.0000123456) sigfig(numbers) # defaults to 3 sigfig(numbers, 3) sigfig(numbers, 1) sigfig(numbers, 6)
Answer by JMT_2080AD for Format numbers to significant figures nicely in R
Another modification on Paul's answer. It appears that it also leaves a trailing decimal. I am removing it with gsub:
sigfig <- function(vec, digits){ return(gsub("\\.$", "", formatC(signif(vec,digits=digits), digits=digits, format="fg", flag="#"))) }
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment