如何禁用科学记数法?

发布于 2024-10-25 01:41:06 字数 734 浏览 2 评论 0原文

我有一个包含 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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

亚希 2024-11-01 01:41:06

您可以使用以下代码有效地删除打印中的科学记数法:

options(scipen=999)

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

options(scipen=999)
柠北森屋 2024-11-01 01:41:06
format(99999999,scientific = FALSE)

给出

99999999
format(99999999,scientific = FALSE)

gives

99999999
感悟人生的甜 2024-11-01 01:41:06

总结所有现有答案

(并添加一些我的观点)

注意:在下面的解释中,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)

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)
⒈起吃苦の倖褔 2024-11-01 01:41:06

我还发现,当我不需要尾随零时,prettyNum(..., science = 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"

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"
指尖凝香 2024-11-01 01:41:06

除了现有的答案之外,例如,如果有人想在整个列上使用前面提到的 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))

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

轻许诺言 2024-11-01 01:41:06

@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

@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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文