我使用内置的t-test函数。
代码如下:
t.test(
mpg$cyl[mpg$model == "a4"],
drill_df$Time_hr[mpg$model == "malibu"],
alternative = "l",
mu = 0,
conf.level = 0.95,
)发布于 2020-09-13 15:41:04
这是一个解决方案。编写一个调用t.test的t_test函数,然后在lapply循环中对数字进行舍入。lapply的返回值是一个列表,因此在返回给调用者之前,必须手动分配类"htest"。
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 https://stackoverflow.com/questions/63867841
复制相似问题