如何禁用科学计数法?

370

我有一个带有一列p值的数据框,我想对这些p值进行筛选。

> 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

选择方式:

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

如果我在R中使用该函数,它运行得非常好。但是,如果我在另一个应用程序(galaxy)中运行它,则不带e-01,例如4.835312e-04的数字将不被排除。

是否有另一种表示p值的方法,例如使用0.0004835312而不是4.835312e-04


7
可能是与Force R not to use exponential notation (e.g. e+10)?重复的问题。 - Scarabee
这是一个重复的内容。 - SabreWolfy
6个回答

683

使用以下代码可以有效地在打印时删除科学计数法:

options(scipen=999)

62
如果你想像我一样将其还原为 :=),默认的 scipen0(请参见 getOption("scipen"))。 - Tomas
37
有没有可能仅在特定命令中使用scipen,例如在print(x, dig=6)中使用?比如summary(m1, scipen=999)print(x, scipen=999)?这将很方便,因为全局设置可能会有问题。 - Tomas
38
答案在这里:https://dev59.com/HmEi5IYBdhLWcg3wMqBy: 使用format(functionResult, scientific=FALSE);as.integer(functionResult);来显示结果,前者可以避免使用科学计数法。 - iNyar
2
@TMS,你如何默认禁用它,以便在新会话打开时不必重新执行命令? - Herman Toothrot
6
R的默认行为旨在简化你的工作,但有时会让人感觉很糟糕。 - zakrapovic
显示剩余3条评论

24
format(99999999,scientific = FALSE)
给予
99999999

2
format(99999999, scientific = FALSE) 将生成 "99999999"(一个字符串字段,而非数字)。 - Dan Tarr
那是正确的,更普遍的答案。 - nd091680

17

总结所有现有答案

(并添加一些我的观点)

注意:在下面的解释中,value是要以某种(整数/浮点)格式表示的数字。

解决方案1:

options(scipen=999)

解决方案2:

format(value, scientific=FALSE);

解决方案3:

as.integer(value);

解决方案4 :

您可以使用不会以科学计数法打印的整数。 您可以在数字后面加上“L”来指定它是一个整数。

paste(100000L)

将会打印100000

解决方案5 :

使用'sprintf()'紧密控制格式

sprintf("%6d", 100000)

将会输出 100000

解决方案6:

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

2

我还发现prettyNum(..., scientific = FALSE)函数在打印时非常有用,当我不想要尾随零时可以使用。请注意,这些函数仅用于打印目的,即这些函数的输出是字符串,而不是数字。

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

@yuskam 把这个放在评论里(即使用withr)。如果你正在开发一个函数/包,这很有帮助。我在这里回答是为了增加那个建议的知名度。

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

我的机器上的输出:

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

0
除了现有的答案之外,如果例如想要在整个列上使用先前提到的format()与dplyr一起使用,则需要将format()包装在lambda函数内部:
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))


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接