首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法控制t-test函数的输出小数位?

有没有办法控制t-test函数的输出小数位?
EN

Stack Overflow用户
提问于 2020-09-13 14:28:47
回答 1查看 272关注 0票数 1

我使用内置的t-test函数。

代码如下:

代码语言:javascript
复制
t.test(
  mpg$cyl[mpg$model == "a4"],
  drill_df$Time_hr[mpg$model == "malibu"],
  alternative = "l",
  mu = 0,
  conf.level = 0.95,
)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-13 15:41:04

这是一个解决方案。编写一个调用t.testt_test函数,然后在lapply循环中对数字进行舍入。lapply的返回值是一个列表,因此在返回给调用者之前,必须手动分配类"htest"

代码语言:javascript
复制
t_test <- function(..., d = 2){
  tt <- t.test(...)
  tt <- lapply(tt, function(x){
    if(is.numeric(x)) round(x, d) else x
  })
  class(tt) <- "htest"
  tt
}

t_test(
  mpg$cyl[mpg$model == "a4"],
  mpg$cyl[mpg$model == "malibu"],
  alternative = "l",
  mu = 0,
  conf.level = 0.95,
)
#   Welch Two Sample t-test
#
#data:  mpg$cyl[mpg$model == "a4"] and mpg$cyl[mpg$model == "malibu"]
#t = -0.54, df = 8.63, p-value = 0.3
#alternative hypothesis: true difference in means is less than 0
#95 percent confidence interval:
# -Inf 0.83
#sample estimates:
#mean of x mean of y 
#     4.86      5.20 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63867841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档