Often you may want to calculate the **exponential** of a number in R.

When we use the term **exponential**, we refer to taking the value **e** in mathematics (which has a value of 2.71828…) and raising it to the power of our specific number.

For example, to take the exponential of the number 5 we would write it as **e ^{5}**, which turns out to be

**148.4132**.

To calculate the exponential of a number in R, you can use the exp() function which uses the following syntax:

**exp(x)**

where:

**x**: The number of interest

This function is built into base R, which means you don’t need to install or load any external packages to use the function.

The following example shows how to use the exp() function in practice in R to calculate the exponential of a number.

**Example: How to Use the exp() Function in R**

Suppose that you would like to calculate the exponential of the number 5 in R.

You can use the following syntax with the **exp()** function to do so:

#calculate exponential of the number 5 exp(5) [1] 148.4132

This returns a value of **148.4132**, which matches the value that we calculated in the intro of this article.

Note that you can also use the **exp()** function to calculate the exponential of all values in a given vector.

For example, we can use the following syntax to create a vector named **my_vector** with values ranging from 1 to 10 and then use the **exp()** function to calculate the exponential of each value in the vector:

#create vector my_vector <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) #calculate exponential of each number in vector exp(my_vector) [1] 2.718282 7.389056 20.085537 54.598150 148.413159 [6] 403.428793 1096.633158 2980.957987 8103.083928 22026.465795

The **exp()** function returns the exponential of each value in the vector.

For example, we can see:

- The exponential of 1 is
**2.718282**. - The exponential of 2 is
**7.389056**. - The exponential of 3 is
**20.085537**. - The exponential of 4 is
**54.598150**.

And so on.

Note that we can use the **round()** function if we’d like to round the number of decimal places to a specific number as well.

For example, we can use the following syntax to calculate the exponential of each value in the vector and round the resulting values to two decimal places:

#create vector my_vector <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) #calculate exponential of each number in vector and round results round(exp(my_vector), 2) [1] 2.72 7.39 20.09 54.60 148.41 403.43 1096.63 2980.96 [9] 8103.08 22026.47

We can see that each value in the output has been rounded to exactly two decimal places, just as we specified.

It’s worth noting that the opposite of calculating the exponential of a number is calculating the log of the number.

Thus, if we calculate the log of a number using the **log()** function in R and then calculate the exponential using the **exp()** function then we will simply end up with the number that we started with:

#create vector my_vector <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) #calculate log and then exponential of each number in vector round(exp(log(my_vector))) [1] 1 2 3 4 5 6 7 8 9 10

Keep this in mind when using the **log()** and **exp()** functions in R.

**Additional Resources**

The following tutorials explain how to perform other common tasks in R:

How to Apply Function to Each Row in R

How to Find Column with Max Value for Each Row in R

How to Use the gregexpr() Function in R