351

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?

Scarabee
  • 5,267
  • 5
  • 26
  • 51
Samantha
  • 3,579
  • 2
  • 14
  • 7

3 Answers3

649

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

options(scipen=999)
Sacha Epskamp
  • 44,659
  • 18
  • 110
  • 130
  • 60
    If you want to revert it back as me :=), the default `scipen` is `0` (see `getOption("scipen")`) – Tomas Jan 28 '13 at 22:59
  • 36
    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
  • 36
    @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
  • `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
14
format(99999999,scientific = FALSE)

gives

99999999
linog
  • 5,350
  • 3
  • 13
  • 23
Peter
  • 1,714
  • 1
  • 15
  • 26
2

I also find 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"

HBat
  • 4,077
  • 4
  • 33
  • 51