我已经从 t-tests 和 ANOVAs 创建了许多输出表,我想除了包含 p 值 (p.value) 的列之外,对表的所有数字列进行四舍五入。
当前代码:
library(dplyr)
library(broom)
a <- rnorm(100, 0.75, 0.1)
t.test <- t.test(a, mu = 0.5, alternative = "greater") %>%
broom::tidy() %>%
mutate_if(is.numeric, round, 2)
Run Code Online (Sandbox Code Playgroud)
问题是这也会舍入我的 p 值,然后显示为 0。我已经有一个函数来报告我的降价文件的 p 值,所以我想知道如何保留 p 值(p.value)未更改但将所有其他数字列四舍五入为 2 位数?
谢谢
如果你真的想圆的所有数值列,但p.value列,只是让你的舒尔p.value列没有得到由四舍五入后取整之前强迫一个字符,然后返回到数字四舍五入。
library(dplyr)
library(broom)
a <- rnorm(100, 0.75, 0.1)
t.test <- t.test(a, mu = 0.5, alternative = "greater") %>%
broom::tidy() %>%
mutate(p.value = as.character(p.value)) %>%
mutate_if(is.numeric, round, 2) %>%
mutate(p.value = as.numeric(p.value))
t.test
# A tibble: 1 x 8
estimate statistic p.value parameter conf.low conf.high method alternative
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 0.76 28.3 1.52e-49 99 0.74 Inf One Sample t-test greater
Run Code Online (Sandbox Code Playgroud)
如果您只想对某些列进行舍入,那么您最好使用mutate_at@user5249203 推广。