简体   繁体   中英

How to assign a value to a variable?

I am wondering how to assign a value to a variable?

For example I perform a simple t.test

" One Sample t-test
data:  FirstExample
t = 19.3645, df = 599, p-value < 2.2e-16
alternative hypothesis: true mean is not equal to 0
90 percent confidence interval:
 0.3522468 0.4177532
sample estimates:
mean of x 
    0.385" 

and I would like to assign the lower confidence interval to a variable:

LowerConf= 0.3522468 

Is there a way to automatically do it?

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.

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']] 

There are three different assignment operators: two of them have leftwards and rightwards forms.

The operators <- and = assign into the environment in which they are evaluated. The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (eg, in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.

The operators <<- and ->> are normally only used in functions, and cause a search to be made through parent environments for an existing definition of the variable being assigned. If such a variable is found (and its binding is not locked) then its value is redefined, otherwise, the assignment takes place in the global environment.

x <- value
x <<- value
value -> x
value ->> x
x = value

Arguments x:a variable name (possibly quoted).

value: a value to be assigned to x.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM