rnorouzian
rnorouzian

Reputation: 7517

Subtract and add a quantity in one line of code in R?

Maybe this is too simple of a question, but I was wondering if it might be possible to have qnorm(.975)*1/sqrt(34 - 3)) be subtracted from and added to tanh(atanh(.5) without writing - and + in two separate lines of code (but instead in one line of code)?

Here is the R code:

tanh(atanh(.5)  -  qnorm(.975)*1/sqrt(34 - 3))
tanh(atanh(.5)  +  qnorm(.975)*1/sqrt(34 - 3))

Upvotes: 4

Views: 139

Answers (2)

rnorouzian
rnorouzian

Reputation: 7517

Upon a bit thinking, the statistical answer is the following:

tanh(atanh(.5) + qnorm(c(.025, .975))*1/sqrt(34 - 3))
#[1] 0.1947659 0.7169429

Upvotes: 3

Rui Barradas
Rui Barradas

Reputation: 76450

R is vectorized. So, take advantage of it.

tanh(atanh(.5) + c(-1, 1)*qnorm(.975)*1/sqrt(34 - 3))
#[1] 0.1947659 0.7169429

The vector c(-1, 1) multiplies the recycled value of qnorm giving a vector of length 2, as required.

Upvotes: 2

Related Questions