Palani Kannan
Palani Kannan

Reputation: 1793

Rename x,y vectors in t-test R

I am performing a t-test in R

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

It gives the result

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 

I need to change the names of the data in result. e.g.,

data: Fruits and Vegetables

Please, anyone give me an idea to include some attribute in t.test to change the data names.

Upvotes: 1

Views: 541

Answers (1)

Gavin Simpson
Gavin Simpson

Reputation: 174928

With some dummy data

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

It would be easier just to rename or create objects with the desired names:

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

But if you really want to do this after the fact, then grab the object returned by t.test():

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

Look at the structure of the object 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"

and note the data.name component. We can replace that with a character string:

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

The print 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

Upvotes: 4

Related Questions