cos()
cos(x) Returns:
numeric · Updated March 13, 2026 · Base Functions trigonometry math cos base
The cos() function computes the trigonometric cosine of numeric values in R. It operates element-wise on vectors and accepts angles in radians.
Syntax
cos(x)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | numeric | — | Numeric vector or single value representing an angle in radians |
Examples
Basic usage
# Compute cosine for a single angle (pi/4 = 45 degrees)
cos(pi / 4)
# [1] 0.7071068
cos(0)
# [1] 1
cos(pi)
# [1] -1
Working with vectors
# Compute cosine for multiple angles
angles <- c(0, pi/6, pi/4, pi/3, pi/2)
cos(angles)
# [1] 1.0000000 0.8660254 0.7071068 0.5000000 0.0000000
Converting degrees to radians
# Convert degrees to radians before using trig functions
deg_to_rad <- function(deg) deg * pi / 180
cos(deg_to_rad(60))
# [1] 0.5
cos(deg_to_rad(180))
# [1] -1
Common Patterns
Creating periodic data
# Generate cosine wave
x <- seq(0, 2 * pi, length.out = 100)
y <- cos(x)
plot(x, y, type = "l")
Circular coordinates
# Calculate y coordinates on a unit circle
theta <- seq(0, 2 * pi, length.out = 9)[-1]
x <- cos(theta)
y <- sin(theta)
data.frame(angle = round(theta, 2), x = round(x, 2), y = round(y, 2))