Open In App

Compute the gamma value of a Non-negative Numeric Vector in R Programming – gamma() Function

Improve
Improve
Like Article
Like
Save
Share
Report

gamma() function in R Language is used to compute the gamma value of a numeric vector using the gamma function.
Gamma function is basically,

gamma(x) = factorial(x - 1)

Syntax: gamma(x)

Parameters:
x: Non-negative Numeric Vector

Example 1:




# R program to calculate the gamma value
  
# Calling gamma Function
gamma(2)
gamma(3)
gamma(5)


Output:

[1] 1
[1] 2
[1] 24

Example 2:




# R program to calculate the gamma value
  
# Creating vectors
x1 <- c(2, 3, 5)
x2 <- c(6, 7, 8)
x3 <- c(-1, -2, -3)
  
# Calling gamma Function
gamma(x1)
gamma(x2)
gamma(x3)


Output:

[1]  1  2 24
[1]  120  720 5040
[1] NaN NaN NaN
Warning message:
In gamma(x3) : NaNs produced

Here, in the above code, the NaN is produced for a Negative numeric vector.


Last Updated : 12 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads