tan()
tan(x) Returns:
numeric · Updated March 13, 2026 · Base Functions trigonometry math tan base
The tan() function computes the trigonometric tangent of numeric values in R. It operates element-wise on vectors and accepts angles in radians.
Syntax
tan(x)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | numeric | — | Numeric vector or single value representing an angle in radians |
Examples
Basic usage
# Compute tangent for a single angle (pi/4 = 45 degrees)
tan(pi / 4)
# [1] 1
tan(0)
# [1] 0
tan(pi)
# [1] 1.224606e-16 # essentially 0
Working with vectors
# Compute tangent for multiple angles
angles <- c(0, pi/6, pi/4, pi/3, pi/2)
tan(angles)
# [1] 0.0000000 0.5773503 1.0000000 1.7320508 Inf
Note that tan(pi/2) returns Infinity because cosine is zero at π/2.
Converting degrees to radians
# Convert degrees to radians before using trig functions
deg_to_rad <- function(deg) deg * pi / 180
tan(deg_to_rad(45))
# [1] 1
tan(deg_to_rad(30))
# [1] 0.5773503
Common Patterns
Creating periodic data
# Generate tangent wave (avoid pi/2 multiples)
x <- seq(0, pi/2 - 0.1, length.out = 50)
y <- tan(x)
plot(x, y, type = "l")