R 如何从t.test输出中消除data.name

R 如何从t.test输出中消除data.name,r,R,我做了这个t.test(在本例中,我将使用iris数据集): >t.test(虹膜$Sepal.Length,虹膜$Sepal.Width) 韦尔奇双样本t检验 数据:鸢尾花$Sepal.Length和鸢尾花$Sepal.Width t=36.463,df=225.68,p值结果$data.name结果 韦尔奇双样本t检验 数据: t=36.463,df=225.68,p值 样本估计: #R> x的平均值y的平均值 #R> 5.843333.057333 您还可以执行以下操作: cat(&quo

我做了这个
t.test
(在本例中,我将使用
iris
数据集):

>t.test(虹膜$Sepal.Length,虹膜$Sepal.Width)
韦尔奇双样本t检验
数据:鸢尾花$Sepal.Length和鸢尾花$Sepal.Width
t=36.463,df=225.68,p值<2.2e-16
替代假设:平均值的真实差异不等于0
95%置信区间:
2.63544 2.93656
样本估计:
x的平均值y的平均值
5.843333  3.057333 
但是,我想删除该调用(t.test的
t.test
输出是一个列表,我想删除列
data.name
,如R文档中所述或通过深入查看该列)

我试着做了两件事(这对我来说是有意义的);我试着使用索引(它完全分解了
t.test
,而不是这个漂亮的文本给出了一个实际的列表)

>结果$data.name结果
韦尔奇双样本t检验
数据:
t=36.463,df=225.68,p值<2.2e-16
替代假设:平均值的真实差异不等于0
95%置信区间:
2.63544 2.93656
样本估计:
x的平均值y的平均值
5.843333  3.057333 
几乎。。。这几乎就是我想要的,但仍然有
数据:
。如果我改为
NA
,它只会说
data:NA


我怎样才能去掉那条线?有什么办法吗?

您可以创建一个新的
print.htest
函数。首先,可以通过调用
getS3method(“print”、“htest”)
找到整个函数。然后删除不需要的行,如下所示:

print.htest 2.63544 2.93656
#R> 样本估计:
#R> x的平均值y的平均值
#R> 5.843333.057333
您还可以执行以下操作:

cat("print.htest<-",
    paste(grep("data|<environment",capture.output(getS3method("print", "htest")), 
               invert = TRUE, value = TRUE), collapse = "\n"), file="temp.R")
source("temp.R")

result <- t.test(iris$Sepal.Length, iris$Sepal.Width)

    Welch Two Sample t-test

t = 36.463, df = 225.68, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 2.63544 2.93656
sample estimates:
mean of x mean of y 
 5.843333  3.057333 

cat(“print.htesthmmm我将编写一个新函数..在任何情况下,我猜都是不寻常的请求。可能会使用assignInNamespace。临时覆盖函数并在.exit返回原始函数。
cat("print.htest<-",
    paste(grep("data|<environment",capture.output(getS3method("print", "htest")), 
               invert = TRUE, value = TRUE), collapse = "\n"), file="temp.R")
source("temp.R")

result <- t.test(iris$Sepal.Length, iris$Sepal.Width)

    Welch Two Sample t-test

t = 36.463, df = 225.68, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 2.63544 2.93656
sample estimates:
mean of x mean of y 
 5.843333  3.057333