370

I have a dataframe with a column of p-values, and I want to make a selection on these p-values.

> pvalues_anova
[1] 9.693919e-01 9.781728e-01 9.918415e-01 9.716883e-01 1.667183e-02
[6] 9.952762e-02 5.386854e-01 9.997699e-01 8.714044e-01 7.211856e-01
[11] 9.536330e-01 9.239667e-01 9.645590e-01 9.478572e-01 6.243775e-01
[16] 5.608563e-01 1.371190e-04 9.601970e-01 9.988648e-01 9.698365e-01
[21] 2.795891e-06 1.290176e-01 7.125751e-01 5.193604e-01 4.835312e-04

Selection way:

anovatest<- results[ - which(results$pvalues_anova < 0.8) ,]

The function works really fine if I use it in R. But if I run it in another application (galaxy), the numbers which don't have e-01 e.g. 4.835312e-04 are not thrown out.

Is there another way to notate p-values, like 0.0004835312 instead of 4.835312e-04?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Samantha
  • 3,785
  • 2
  • 15
  • 7

6 Answers6

679

You can effectively remove scientific notation in printing with this code:

options(scipen=999)
Sacha Epskamp
  • 46,463
  • 20
  • 113
  • 131
  • 62
    If you want to revert it back as me :=), the default `scipen` is `0` (see `getOption("scipen")`) – Tomas Jan 28 '13 at 22:59
  • 37
    Is there any possibility to use `scipen` only in one particular command, like in `print(x, dig = 6)`? Such as `summary(m1, scipen = 999)` or `print(x, scipen = 999)`? That would be cool. Because the global setting might be problematic. – Tomas Jan 28 '13 at 23:01
  • 38
    @TMS: The answer is here: http://stackoverflow.com/questions/21509346/r-displays-numbers-in-scientific-notation: ``format(functionResult, scientific=FALSE);`` or ``as.integer(functionResult);`` – iNyar Jul 03 '15 at 13:02
  • 2
    @TMS how do you disable it by default so when a new session opens you don't have to redo the command? – Herman Toothrot Apr 04 '17 at 14:45
  • 5
    The R default behavior that want to simplify your life makes it hell – zakrapovic Aug 17 '17 at 15:42
  • @user4050 I reckon one could put the command in their .Rprofile. On linux (and maybe Mac?), one would make a file called .Rprofile in their home dir with that line in it. If you're one Windows, it looks like there's instructions here: http://www.statmethods.net/interface/customizing.html – John Madden Oct 11 '17 at 13:01
  • @HermanToothrot - this two minute video explains how to do that: https://www.youtube.com/watch?v=KazWu6i3NI0&list=PLcgz5kNZFCkzSyBG3H-rUaPHoBXgijHfC&index=91&t=0s – MatthewR Jun 10 '18 at 00:37
  • 1
    `withr` package has a handy function called `with_options` or `local_options` to revert back to default automatically – yuskam Jan 13 '22 at 12:12
23
format(99999999,scientific = FALSE)

gives

99999999
linog
  • 5,786
  • 3
  • 14
  • 28
Peter
  • 2,120
  • 2
  • 19
  • 33
14

Summarising all existing answers

(And adding a few of my points)

Note : In the below explanation, value is the number to be represented in some (integer/float) format.

Solution 1 :

options(scipen=999)

Solution 2 :

format(value, scientific=FALSE);

Solution 3 :

as.integer(value);

Solution 4 :

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

paste(100000L)

will print 100000

Solution 5 :

Control formatting tightly using 'sprintf()'

sprintf("%6d", 100000)

will print 100000

Solution 6 :

prettyNum(value, scientific = FALSE, digits = 16)
2

I also find the prettyNum(..., scientific = FALSE) function useful for printing when I don't want trailing zeros. Note that these functions are useful for printing purposes, i.e., the output of these functions are strings, not numbers.

p_value <- c(2.45496e-5, 3e-17, 5.002e-5, 0.3, 123456789.123456789)
format(p_value, scientific = FALSE)
#> [1] "        0.00002454960000000" "        0.00000000000000003"
#> [3] "        0.00005002000000000" "        0.29999999999999999"
#> [5] "123456789.12345679104328156"


format(p_value, scientific = FALSE, drop0trailing = TRUE)
#> [1] "        0.0000245496"        "        0.00000000000000003"
#> [3] "        0.00005002"          "        0.29999999999999999"
#> [5] "123456789.12345679104328156"


# Please note that the last number's last two digits are rounded:
prettyNum(p_value, scientific = FALSE, digits = 16)
#> [1] "0.0000245496"        "0.00000000000000003" "0.00005002"
#> [4] "0.3"                 "123456789.1234568"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HBat
  • 4,873
  • 4
  • 39
  • 56
0

In addition to the existing answers, if, for example, one would like to use the previously mentioned format() with dplyr on the whole column, then format() needs to be wrapped inside the lambda function:

colors <- c("red", "green", "blue", "yellow", "orange")
floats <- runif(5) / 1000000

df <- data.frame(colors, floats) %>% 
  dplyr::mutate_if(is.numeric, function(x) format(x, scientific = FALSE))

0

@yuskam put this in a comment (i.e. use withr) further up. If you're developing a function/package this is helpful. I am answering here to increase the profile of that suggestion.

# show standard setting to confirm ground truth
getOption("scipen")

vect <- c(4.835312e-06, 4.835312e-05, 4.835312e-04)
print(vect)

print_non_scientific <- function(x) {
  # show setting within function
  withr::local_options(list(scipen = 999))
  print(getOption("scipen"))
  print(x)
}

print_non_scientific(vect)

# show standard setting to confirm that the function did not change it
getOption("scipen")

Output on my machine:

> # show standard setting to confirm ground truth
> getOption("scipen")
[1] 0
> 
> vect <- c(4.835312e-06, 4.835312e-05, 4.835312e-04)
> print(vect)
[1] 4.835312e-06 4.835312e-05 4.835312e-04
> 
> print_non_scientific <- function(x) {
+   # show setting within function
+   withr::local_options(list(scipen = 999))
+   print(getOption("scipen"))
+   print(x)
+ }
> 
> print_non_scientific(vect)
[1] 999
[1] 0.000004835312 0.000048353120 0.000483531200
> 
> # show standard setting to confirm that the function did not change it
> getOption("scipen")
[1] 0
Patrick
  • 742
  • 7
  • 19