371

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?

2

6 Answers 6

684

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

options(scipen=999)
8
  • 62
    If you want to revert it back as me :=), the default scipen is 0 (see getOption("scipen"))
    – Tomas
    Jan 28, 2013 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, 2013 at 23:01
  • 38
    @TMS: The answer is here: stackoverflow.com/questions/21509346/…: format(functionResult, scientific=FALSE); or as.integer(functionResult);
    – iNyar
    Jul 3, 2015 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? Apr 4, 2017 at 14:45
  • 6
    The R default behavior that want to simplify your life makes it hell
    – zakrapovic
    Aug 17, 2017 at 15:42
24
format(99999999,scientific = FALSE)

gives

99999999
2
  • 2
    format(99999999,scientific = FALSE) will generate "99999999" (a character field, not numeric)
    – Dan Tarr
    Apr 28, 2022 at 1:26
  • That's the correct and more general answer
    – nd091680
    Nov 28, 2022 at 13:14
17

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"
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.