在t检验R中重命名x,y向量

时间:2012-06-12 10:15:57

标签: r

我正在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中包含一些属性来更改数据名称。

1 个答案:

答案 0 :(得分: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