如何将科学记数法转换为小数?
How to convert scientific notation to decimal in tibbles?
虽然基本,但我无法在任何地方找到答案:如何禁用小标题中的科学记数法,并让小标题显示小数?
我的数据
我有一个简单的小标题,来自 lm() %>% broom::tidy()
。
library(tidyverse)
## I used dput() to get this:
tidy_lm_output <- structure(list(term = c("(Intercept)", "mood", "sleep"), estimate = c(-0.00000000000000028697849703988,
-0.0746522106739049, 0.835867664974019), std.error = c(0.0319620048196539,
0.0464197056030362, 0.0464197056030362), statistic = c(-0.00000000000000897873893265334,
-1.60820086435494, 18.006742053085), p.value = c(0.999999999999993,
0.108628280589954, 9.41480010964234e-53)), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame"))
> tidy_lm_output
## # A tibble: 3 x 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) -2.87e-16 0.0320 -8.98e-15 10.00e- 1
## 2 mood -7.47e- 2 0.0464 -1.61e+ 0 1.09e- 1
## 3 sleep 8.36e- 1 0.0464 1.80e+ 1 9.41e-53
奇怪的是,只有"std.error"栏显示的是小数,其他栏都是科学计数法。
我想让所有列都以小数形式显示信息,同时仍然显示为小数。
尝试解决此问题
options(scipen=999)
的 go-to 解决方案无效。
- 使用
format(scientific = FALSE)
也没有用。
这个问题在别处问过
- 有人问 here 但仍未得到答复。
您可以使用 options(pillar.sigfig = 5)
来控制打印的有效位数。
tidy_lm_output
# A tibble: 3 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) -2.8698e-16 0.031962 -8.9787e-15 1.0000e+ 0
2 mood -7.4652e- 2 0.046420 -1.6082e+ 0 1.0863e- 1
3 sleep 8.3587e- 1 0.046420 1.8007e+ 1 9.4148e-53
有关如何设置 tibbles 格式的更多信息,请参阅 help(format.tbl)
但是,如果您只想四舍五入这些数字,您可以这样做:
tidy_lm_output %>%
mutate_if(is.numeric, round, 5)
# A tibble: 3 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 0 0.0320 0 1
2 mood -0.0746 0.0464 -1.61 0.109
3 sleep 0.836 0.0464 18.0 0
format
是一个 base 函数,不支持小标题。所以要使用它,首先将您的小标题转换为数据框:
Tidyverse 管道样式
https://style.tidyverse.org/pipes.html
my_formatted_df <- my_tibble %>% as.data.frame() %>% format(scientific=FALSE)
嵌套函数样式
my_formatted_df <- format(as.data.frame(my_tibble), scientific = FALSE)
虽然基本,但我无法在任何地方找到答案:如何禁用小标题中的科学记数法,并让小标题显示小数?
我的数据
我有一个简单的小标题,来自 lm() %>% broom::tidy()
。
library(tidyverse)
## I used dput() to get this:
tidy_lm_output <- structure(list(term = c("(Intercept)", "mood", "sleep"), estimate = c(-0.00000000000000028697849703988,
-0.0746522106739049, 0.835867664974019), std.error = c(0.0319620048196539,
0.0464197056030362, 0.0464197056030362), statistic = c(-0.00000000000000897873893265334,
-1.60820086435494, 18.006742053085), p.value = c(0.999999999999993,
0.108628280589954, 9.41480010964234e-53)), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame"))
> tidy_lm_output
## # A tibble: 3 x 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) -2.87e-16 0.0320 -8.98e-15 10.00e- 1
## 2 mood -7.47e- 2 0.0464 -1.61e+ 0 1.09e- 1
## 3 sleep 8.36e- 1 0.0464 1.80e+ 1 9.41e-53
奇怪的是,只有"std.error"栏显示的是小数,其他栏都是科学计数法。
我想让所有列都以小数形式显示信息,同时仍然显示为小数。
尝试解决此问题
options(scipen=999)
的 go-to 解决方案无效。- 使用
format(scientific = FALSE)
也没有用。
这个问题在别处问过
- 有人问 here 但仍未得到答复。
您可以使用 options(pillar.sigfig = 5)
来控制打印的有效位数。
tidy_lm_output
# A tibble: 3 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) -2.8698e-16 0.031962 -8.9787e-15 1.0000e+ 0
2 mood -7.4652e- 2 0.046420 -1.6082e+ 0 1.0863e- 1
3 sleep 8.3587e- 1 0.046420 1.8007e+ 1 9.4148e-53
有关如何设置 tibbles 格式的更多信息,请参阅 help(format.tbl)
但是,如果您只想四舍五入这些数字,您可以这样做:
tidy_lm_output %>%
mutate_if(is.numeric, round, 5)
# A tibble: 3 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 0 0.0320 0 1
2 mood -0.0746 0.0464 -1.61 0.109
3 sleep 0.836 0.0464 18.0 0
format
是一个 base 函数,不支持小标题。所以要使用它,首先将您的小标题转换为数据框:
Tidyverse 管道样式
https://style.tidyverse.org/pipes.html
my_formatted_df <- my_tibble %>% as.data.frame() %>% format(scientific=FALSE)
嵌套函数样式
my_formatted_df <- format(as.data.frame(my_tibble), scientific = FALSE)