Use scientific notation with xtable in R - r

I pass a data.frame to xtable
dat.table <- xtable(dat[1:20,] ,digits=10)
Instead of displaying digits like that, I would prefer to use scientific notation. How would I do that?
had a look but all I found was R: formatting the digits in xtable which isn't the answer it seems.

Try:
dat.table <- xtable(dat[1:20,] ,digits=-10)
"If values of digits are negative, the corresponding values of x are displayed in scientific format with abs(digits) digits." xtable

If you are wanting to x10^ notation trying use print and xtable. something like:
print(xtable(dat[1:10,1:7], display=c("s","s", "s","s","g","g","g","g")), math.style.exponents = TRUE)
where s is string and g is used for scientific notation (only when space is saved), themath.style.exponents from print will convert to x10^format.

Related

How can I set a comma separator for thousands as a default option for how the interpreter in R presents numbers?

I dont want a function. I just want to have that be the default way in which the R interpreter always displays numbers. Thanks in advance.
While I'm not aware of a way to have your numbers always display with commas, there is a way to turn-off scientific notation for your session and then format your numerical output to show commas as a string.
Here's one possible solution:
# Load library
load(scales)
# Turn-off scientific notation for your R session
options(scipen = 999)
# An example vector of big numbers
x = c(1000000000000000, 2000000000000, 3000000000000)
# Use the scales::comma() function to add commas
# Output will be formated as a string
comma(x)
#> [1] "1,000,000,000,000,000" "2,000,000,000,000" "3,000,000,000,000"

Convert superscripted numbers from string into scientific notation (from Unicode, UTF8)

I imported a vector of p-values from an Excel table. The numbers are given as superscripted Unicode strings. After hours of trying I still struggle to convert them into numbers.
See example below. Simple conversion with as.numeric() doesn't work. I also tried to use Regex to capture the superscripted numbers, but it turned out that each superscripted number has a distinct Unicode code, for which there is no translation.
test <- c("0.0126", "0.000289", "4.26x10⁻¹⁴", "6.36x10⁻⁴⁸",
"4.35x10⁻⁹", "0.115", "0.0982", "0.000187", "0.0484", "0.000223")
as.numeric(test)
Does somebody know of an R-package which could do the translation painlessly, or do I have to translate the codes one by one into digits?
This kind of formatting is definitely not very portable... Here's one possible solution though, for the exercise...
test <- c("0.0126", "0.000289", "4.26x10⁻¹⁴", "6.36x10⁻⁴⁸",
"4.35x10⁻⁹", "0.115", "0.0982", "0.000187", "0.0484",
"0.000223")
library(utf8)
library(stringr)
# normalize, ie everything to "normal text"
testnorm <- utf8_normalize(test, map_case = TRUE, map_compat = TRUE)
# replace exponent part
# \\N{Minus Sign} is the unicode name of the minus sign symbol
# (see [ICU regex](http://userguide.icu-project.org/strings/regexp))
# it is necessary because the "-" is not a plain text minus sign...
testnorm <- str_replace_all(testnorm, "x10\\N{Minus Sign}", "e-")
# evaluate these character strings
p_vals <- sapply(X = testnorm,
FUN = function(x) eval(parse(text = x)),
USE.NAMES = FALSE
)
# everything got adjusted to the "e-48" element...
format(p_vals, digits = 2, scientific = F)

Function write() inconsistent with number notation

Consider the following script:
list_of_numbers <- as.numeric()
for(i in 1001999498:1002000501){
list_of_numbers <- c(list_of_numbers, i)
}
write(list_of_numbers, file = "./list_of_numbers", ncolumns = 1)
The file that is produced looks like this:
[user#pc ~]$ cat list_of_numbers
1001999498
1001999499
1.002e+09
...
1.002e+09
1.002e+09
1.002e+09
1002000501
I found a couple more ranges where R does not print consistently the number format.
Now I have the following questions:
Is this a bug or is there an actual reason for this behavior?
Why just in certain ranges, why not every number above x?
I know how I can solve this like this:
options(scipen = 1000)
But are there more elegant ways than setting global options? Without converting it to a dataframe and changing the format.
It's not a bug, R chooses the shortest representation.
More precisely, in ?options one can read:
fixed notation will be preferred unless it is more than scipen
digits wider.
So when scipen is 0 (the default), the shortest notation is preferred.
Note that you can get the scientific notation of a number x with format(x, scientific = TRUE).
In your case:
1001999499 is 10 characters long whereas its scientific notation 1.001999e+09 is longer (12 characters), so the decimal notation is kept.
1001999500: scientific notation is 1.002e+09, which is shorter.
..................... (scientific notation stays equal to 1.002e+09, hence shorter)
1002000501: 1.002001e+09 is longer.
You may ask: how come that 1001999500 is formatted as 1.002e+09 and not as 1.0019995e+09? It's simply because there is also an option that controls the number of significant digits. It is named digits and its default value is 7. Since 1.0019995 has 8 significant digits, it is rounded up to 1.002.
The simplest way to ensure that decimal notation is kept without changing global options is probably to use format:
write(format(list_of_numbers, scientific = FALSE, trim = TRUE),
file = "./list_of_numbers")
Side note: you didn't need a loop to generate your list_of_numbers (which by the way is not a list but a vector). Simply use:
list_of_numbers <- as.numeric(1001999498:1002000501)

How do I force R to write out scientific notation with 3 significant figures after a decimal?

Because Fortran, I need to write a program that, among other wonderful features, will force R to output a number in scientific notation with 3 significant figures after the decimal point.
Specifically, I need 170.5 to be written to output as 1.705e+02.
Using options(scipen = -999), I can force r to write 170.5 as 1.71e+02. However, this is insufficient for my use - ireally need that third digit after the period.
I think this question ought to be easy, but I am unable to figure out how to solve it. Thanks.
Perhaps try
options(scipen = -999, digits = 4)
Look at either sprintf or formatC.
sprintf("%s", 170.5)
[1] "1.705e+02"
print( sprintf("%s", 170.5), quote=FALSE)
[1] 1.705e+02

convert number from scientific notation in R

I have a numeric variable imported from Oracle with 17 numbers, for example: 20172334534654667.
Now I imported it from Oracle using dbGetQuery() in R, but R use scientific notation: 2.01723e+16
If I try to convert the number using:
mydata$var <- format(mydata$a, scientific=FALSE)
I obtain 20172334534654600 instead of 20172334534654667
So, the last two numbers are always substituted with 00.
How can I solve it, possibly without using additional packages?
I was unable to replicate your issue, but I think it would probably be best to use formatC rather than format.
For your case, it could be:
numb <- 20172334534654667
numb
formatC(numb, format = "f", digits = 0)
Which gives:
[1] "20172334534654668"
Hopefully that works for you!

Resources