How to Use the tan() Function in R

The tan() function in R is “used to calculate the tangent of the numeric value”. The function accepts a numeric value as a parameter and returns the tangent value.

Syntax

tan(number)

Parameters

number: It is a numeric value.

Return value

The tan() function returns the tangent of a number (in radians) sent as a parameter.

Example 1: How to use the tan() function in R

x1 <- -45
x2 <- -60

tan(x1)
tan(x2)

Output

[1] -1.619775
[1] -0.3200404

Example 2: Passing the pi to the tan() function

The pi is a built-in constant whose value is 3.141593. Let’s pass the pi to the tan() function and see the output.

# Positive number in radians
data <- tan(2.1)
print(paste0("tan(2.1): ", data))

# Negative number in radians
data_a <- tan(-1.9)
print(paste0("tan(-1.9): ", data_a))

# Converting the degrees angle into radians and then applying tan()
data_b <- tan(45 * (pi / (180)))
print(paste0("tan(45 * (pi / (180))): ", data_b))

Output

[1] "tan(2.1): -1.70984654290451"
[1] "tan(-1.9): 2.92709751467777"
[1] "tan(45 * (pi / (180))): 1"

Example 3: Using a tan() function with Vector

dt <- c(1, 0.5, -1, -0.25, 0.5, 2 / 3)
tanpi(dt)

Output

[1] 0.000000  NaN  0.000000  -1.000000  NaN  -1.732051
Warning message:
In tanpi(dt) : NaNs produced

That is it.

Leave a Comment