Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R exp() Function - Calculating Exponentials

R exp() Function - Calculating Exponentials

R Language Examples R Language Examples

The R exp() function is used to compute e raised to the power of x, that is, the natural exponential function.

exp() is the inverse of the natural logarithm log(), and is widely used in scenarios such as exponential growth models and logistic regression.

The syntax of the exp() function is as follows:

exp(x)

Parameter Description:

Example

Compute the power of e

print

(

exp

(

1

)

)

   

e^1 = e

print

(

exp

(

2

)

)

   

e^2

print

(

exp

(

0

)

)

   

e^0 = 1

Vector operations

x

<-

c

(

0

,

1

,

2

,

3

)

result.

exp

<-

exp

(

x

)

print

(

result.

exp

)

exp and log are inverse operations of each other

print

(

exp

(

log

(

5

)

)

)

 

e^(ln(5)) = 5

print

(

log

(

exp

(

5

)

)

)

 

ln(e^5) = 5

The output of the code above is:

[1] 2.718282

[1] 7.389056

[1] 1

[1]  1.000000  2.718282  7.389056 20.085537

[1] 5

[1] 5

exp() is often used to convert log-odds back into probabilities:

Example

log-odds in logistic regression

log_odds

<-

c

(

2

,

1

,

0

,

1

,

2

)

Convert log-odds to probability

Probability = exp(log_odds) / (1 + exp(log_odds))

prob

<-

exp

(

log_odds

)

/

(

1

exp

(

log_odds

)

)

print

(

round

(

prob,

4

)

)

The output of the code above is:

[1] 0.1192 0.2689 0.5000 0.7311 0.8808

R Language Examples R Language Examples