R Advanced

exp() Function: Calculate Exponential of a Number in R

The exp() is a built-in function that calculates the exponential of its input, raising Euler’s number e (approximately 2.718281828) to the power of the given value.

For example, e^y or the exponential of y. The value of e is approximately equal to 2.71828.

data <- 1
info <- -2
stock <- 1.9

exp(data)

# Output: [1] 2.718282

exp(info)

# Output: [1] 0.1353353

exp(stock)

# Output: [1] 6.685894

In this code, e^1 returns 2.718282.

Even if the input is -2, e^-2 returns a positive value 0.1353353

In the last case, the input is 1.9, e^1.9 returns a positive value 6.685894

Syntax

exp(x)

Parameters

Argument Description
x (required) It represents the input value to calculate the exponential.

It is a single value, whether it is a real or complex value.

Exponential of a Vector

The exp() function is vectorized, meaning that if the input is a vector, the output will also be a vector, where each value is the exponential value of the corresponding input value.

# Create a vector of numbers
vec <- c(1, 2, 3)

# Calculate the exponential of each element in the vector
exp_vec <- exp(vec)

# Print the results
print(exp_vec)

# Output: [1] 2.718282  7.389056  20.085537

Exponential of a data frame

When it comes to a data frame, each column is a vector. So, you need to pass a specific column to the exp() function that will return the exponential value of that column.

For selecting a specific column, you can use the $ operator.

df <- data.frame(
  col1 = c(1, 2, 3),
  col2 = c(4, 5, 6),
  col3 = c(7, 8, 9)
)

# Calculate exponential of the 'col2' column
df$col2_values <- exp(df$col2)

# Print the data frame
print(df)

# Output:
#    col1  col2  col3   col2_values
# 1   1     4     7      54.59815
# 2   2     5     8      148.41316
# 3   3     6     9      403.42879

Matrix Input

If the input is a matrix, let’s say a 2×2 matrix, the output will also be a 2×2 matrix.

mat <- matrix(c(0, 1, -1, 2), nrow = 2)
exp(mat)

# Output:
#         [,1]           [,2]
# [1,]     1           0.3678794
# [2,]     2.718282    7.389056

Exponential value of pi

The value of pi is 3.141593.

exp(pi)

# Output: [1] 23.14069

Let’s plot the line chart from -pi to +pi.

y <- seq(-pi, pi, by = 0.1)

plot(y, exp(y), typ = "l", col = "green")

Output

Plotting the exponential value in the range of -4 ~ +4

y <- seq(-4, 4, by = 0.1)
plot(y, exp(y), typ = "l", col = "green")

Output

You can see that the value and the exponent growth increase exponentially in the line chart.

Combining with rounding

The exp() method returns a float value, and if you want to round the numbers, you can use the round() method for the formatted output.

expo <- exp(c(1, 2))

rounded_expo <- round(expo, 2)

print(rounded_expo)

# Output: [1] 2.72   7.39

Edge cases

What if the input value is 0, inf, -inf, or NaN? What should be the output? Let’s find out.

exp(0)

# Output: 1

exp(Inf)

# Output: Inf

exp(-Inf)

# Output: 0

exp(NaN)

# Output: NaN

The exponential of 0 is 1,  Infinity is Inf, -Infinity is -Inf, and NaN is NaN.

Recent Posts

file.rename(): Renaming Single and Multiple Files in R

To rename a file in R, you can use the file.rename() function. It renames a…

4 hours ago

R prop.table() Function

The prop.table() function in R calculates the proportion or relative frequency of values in a…

10 hours ago

R split() Function: Splitting a Data

The split() function divides the input data into groups based on some criteria, typically specified…

1 week ago

colMeans(): Calculating the Mean of Columns in R Data Frame

The colMeans() function in R calculates the arithmetic mean of columns in a numeric matrix,…

2 weeks ago

rowMeans(): Calculating the Mean of rows of a Data Frame in R

The rowMeans() is a built-in, highly vectorized function in R that computes the arithmetic mean…

3 weeks ago

colSums(): Calculating the Sum of Columns of a Data Frame in R

The colSums() function in R calculates the sums of columns for numeric matrices, data frames,…

3 weeks ago