How to assign a value to a variable?

In general you assign a value exactly the way you’ve shown, using variable = value . However, you are dealing with the result of a t-test, where the result is a more complex value.

You can still assign the result of the t-test though:

result = t.test(a) 

Now the question becomes: how to extract the confidence interval (and its lower bound)?

You can examine which values result stores via names(result) :

names(result) # [1] "statistic" "parameter" "p.value" "conf.int" "estimate" # [6] "null.value" "alternative" "method" "data.name" 

So there we go: the value you want is conf.int . You get it by subsetting the result:

result$conf.int # [1] 0.3522468 0.4177532 # attr(,"conf.level") # [1] 0.95 

And you can assign this value to a variable as usual:

lower_conf = result$conf.int[1] # 1 is lower, 2 is upper bound. 

If you only need the confidence interval from the test (although that’s a bit weird), you can also assign the value directly, without an intermediate result variable:

lower_conf = t.test(a)$conf.int[1] 

Check the documentation on $ (this can be done in R via ?`$` ) for more details.

answered Mar 23, 2014 at 14:07 Konrad Rudolph Konrad Rudolph 542k 136 136 gold badges 954 954 silver badges 1.2k 1.2k bronze badges

A general advice to inspect objects in R is to use str :

str(a) List of 9 $ statistic : Named num -5.43 ..- attr(*, "names")= chr "t" $ parameter : Named num 22 ..- attr(*, "names")= chr "df" $ p.value : num 1.86e-05 $ conf.int : atomic [1:2] -11.05 -4.95 ..- attr(*, "conf.level")= num 0.95 $ estimate : Named num [1:2] 5.5 13.5 ..- attr(*, "names")= chr [1:2] "mean of x" "mean of y" $ null.value : Named num 0 ..- attr(*, "names")= chr "difference in means" $ alternative: chr "two.sided" $ method : chr "Welch Two Sample t-test" $ data.name : chr "1:10 and c(7:20)" - attr(*, "class")= chr "htest" 

Then here you go , the object is a list that you can subset using $ (in the console) or using [ and/or [[ in your script. For example:

a[['conf.int']]