r - 如何打印具有 2 个有效数字的 p 值?
问题描述
当我通过执行以下操作从我的 t.test 打印我的 p 值时:
ttest_bb[3]
它返回完整的 p 值。我怎样才能使它只打印前两个整数?即.03
而不是.034587297
?
解决方案
t.test 的输出是一个列表。如果您仅用于[
获取 p 值,则返回的是一个包含一个元素的列表。如果[[
要将其视为向量,则要使用 t.test 返回的列表中的位置获取包含的元素。
> ttest_bb <- t.test(rnorm(20), rnorm(20))
> ttest_bb
Welch Two Sample t-test
data: rnorm(20) and rnorm(20)
t = -2.5027, df = 37.82, p-value = 0.01677
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.4193002 -0.1498456
sample estimates:
mean of x mean of y
-0.3727489 0.4118240
> # Notice that what is returned when subsetting like this is
> # a list with the name p.value
> ttest_bb[3]
$`p.value`
[1] 0.01676605
> # If we use the double parens then it extracts just the vector contained
> ttest_bb[[3]]
[1] 0.01676605
> # What you're seeing is this:
> round(ttest_bb[3])
Error in round(ttest_bb[3]) :
non-numeric argument to mathematical function
> # If you use double parens you can use that value
> round(ttest_bb[[3]],2)
[1] 0.02
> # I prefer using the named argument to make it more clear what you're grabbing
> ttest_bb$p.value
[1] 0.01676605
> round(ttest_bb$p.value, 2)
[1] 0.02
推荐阅读
- class - 是否可以将一个类的代码分发到多个文件?
- database - 如何以 oracle home 用户身份登录 oracleDB?
- python - 我在哪里可以获得 Werkzeug 的 send_file 的“环境”参数?
- java - 如何以编程方式折叠android中的通知面板
- azure - DBFS AZURE Databricks - 文件存储和 DBFS 的区别
- ios - 适用于英特尔 mac 与 M1 mac 的 iOS 构建和 Mac 构建
- java - jdbctemplate 动态查询参数限制是多少?
- mysql - #1064-您的 SQL 语法有错误:查看与您的 MySQL 服务器版本相对应的手册,以获取在 ') 附近使用的正确语法
- jupyter-notebook - 无法在 Jupyter 中将字符串转换为数字
- git - 错误:“src/”没有检查出的提交致命:添加文件失败