The tanh() function in R “calculates the number’s hyperbolic tangent in radians”. It takes a numerical value, vector, or array and returns the hyperbolic tangent.
Syntax
tanh(num)
Parameters
num: It is a numeric value.
Return value
It returns the hyperbolic tangent of a number (in radians).
Example 1: How to use the tanh() function in R
tanh(1)
Output
[1] 0.7615942
If you pass the 0 to the tanh() function, it will return 0.
tanh(0)
Output
[1] 0
Example 2: Using the tanh() function to a Vector
To create a Vector in R, use the c() function. Then pass that vector to the tanh() function.
rv <- c(-1, 0.5, 0, 0.5, 1)
tanh(rv)
Output
[1] -0.7615942 0.4621172 0.0000000 0.4621172 0.7615942
Example 3: Passing a pi, positive, and negative number
The pi is a built-in constant in R; its value is 3.141593.
data_one <- tanh(0.9)
print(paste0("tanh(0.9): ", data_one, " radians"))
data_two <- tanh(-0.75)
print(paste0("tanh(-0.75): ", data_two, " radians"))
data_three <- tanh(0.45) * (180.0 / pi)
print(paste0("tanh(0.45): ", data_three, " degrees"))
Output
[1] "tanh(0.9): 0.716297870199024 radians"
[1] "tanh(-0.75): -0.635148952387287 radians"
[1] "tanh(0.45): 24.1730323815932 degrees"
Plot the tanh() function to a graph
We can use the seq() function to create a series of values and pass that to the plot() function, creating a line chart.
dt <- seq(-1, 1, by = 0.05)
plot(dt, tanh(dt), typ = "l", col = "red")
abline(v = 0, lty = 6, col = "blue")
Output
That is it for the tanh() function in R.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.