1

我正在 R 中进行 t 检验

out <- t.test(x=input1, y=input2, alternative=c("two.sided","less","greater"), mu=0, paired=TRUE, conf.level = 0.95)

它给出了结果

Paired t-test

data:  input1 and input2
t = -1.1469, df = 7, p-value = 0.2891
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 -0.15100900  0.05236717 
sample estimates:
mean of the differences 
            -0.04932091 

我需要更改结果中数据的名称。例如,

数据:水果和蔬菜

请任何人给我一个想法,在 t.test 中包含一些属性来更改数据名称。

4

1 回答 1

4

带有一些虚拟数据

set.seed(1)
input1 <- rnorm(20, mean = -1)
input2 <- rnorm(20, mean = 5)

重命名或创建具有所需名称的对象会更容易:

Fruits <- input1
Vegetables <- input2

t.test(x = Fruits, y = Vegetables, paired = TRUE, alternative = "two.sided")

    Paired t-test

data:  Fruits and Vegetables 
t = -18.6347, df = 19, p-value = 1.147e-13
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 -6.454791 -5.151218 
sample estimates:
mean of the differences 
              -5.803005

但是如果你真的想在事后这样做,那么抓住返回的对象t.test()

tmp <- t.test(x = input1, y = input2, paired = TRUE, alternative = "two.sided")

看对象的结构tmp

> str(tmp)
List of 9
 $ statistic  : Named num -18.6
  ..- attr(*, "names")= chr "t"
 $ parameter  : Named num 19
  ..- attr(*, "names")= chr "df"
 $ p.value    : num 1.15e-13
 $ conf.int   : atomic [1:2] -6.45 -5.15
  ..- attr(*, "conf.level")= num 0.95
 $ estimate   : Named num -5.8
  ..- attr(*, "names")= chr "mean of the differences"
 $ null.value : Named num 0
  ..- attr(*, "names")= chr "difference in means"
 $ alternative: chr "two.sided"
 $ method     : chr "Paired t-test"
 $ data.name  : chr "input1 and input2"
 - attr(*, "class")= chr "htest"

并注意data.name组件。我们可以用字符串替换它:

tmp$data.name <- "Fuits and Vegetables"

印刷品tmp

> tmp

    Paired t-test

data:  Fuits and Vegetables 
t = -18.6347, df = 19, p-value = 1.147e-13
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 -6.454791 -5.151218 
sample estimates:
mean of the differences 
              -5.803005
于 2012-06-12T10:29:50.850 回答