rguides

cos()

cos(x)

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

ParameterTypeDefaultDescription
xnumericNumeric vector or single value representing an angle in radians

The parameter x is always interpreted in radians, which means common angle values like 45° or 90° must be converted before passing them to cos(). Multiply degrees by pi / 180 to get radians, or define a small helper function if your workflow involves degree-based measurements frequently.

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

The three lines above demonstrate key values of cosine on the unit circle. At angle 0 radians the cosine is 1, at π/4 it is roughly 0.707, and at π radians it reaches -1. Cosine returns values in the closed interval [-1, 1] for any real input, which makes it useful for normalizing directional data and generating waveforms.

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

When you pass a vector to cos(), R applies the function to each element independently and returns a vector of the same length. This vectorized behavior means you can compute cosine for hundreds or thousands of angles in a single call without writing an explicit loop, which is both cleaner and much faster than iterating element by element.

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

The helper function deg_to_rad() encapsulates the conversion formula so you never have to type * pi / 180 inline. This pattern is useful because most real-world angle data arrives in degrees, whether from survey instruments, geographic coordinates, or user-facing interfaces. Keeping the conversion in one place avoids errors and makes the intent clear.

Common patterns

Creating periodic data

# Generate cosine wave
x <- seq(0, 2 * pi, length.out = 100)
y <- cos(x)
plot(x, y, type = "l")

Generating a smooth cosine wave with seq() and plot() is a quick way to visualize the function’s shape. The length.out argument controls how many points are sampled across the domain, with higher values producing a smoother curve. This technique generalizes to any mathematical function you want to inspect graphically before using it in a statistical model or simulation.

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))

cos() in data analysis

cos() accepts angles in radians, not degrees. To convert: multiply degrees by pi / 180. The full degree-to-cosine pipeline is cos(degrees * pi / 180).

In data analysis, cos() appears most often in:

  • Circular statistics (wind direction, hourly patterns) where the cosine and sine of an angle together represent direction on a unit circle
  • Signal processing and Fourier transforms where cosine and sine are basis functions
  • Similarity measures: cosine similarity between two vectors is sum(a * b) / (sqrt(sum(a^2)) * sqrt(sum(b^2))), which measures the angle between them

The output of cos() is always in the range [-1, 1]. cos(0) is 1, cos(pi/2) is approximately 0 (not exactly, due to floating-point), and cos(pi) is -1.

cos() returns exactly 1 for x = 0 and is very close to 0 for x = pi/2, but not exactly zero due to floating-point representation. Use round(cos(pi/2), 10) or all.equal() for comparisons near zero. For a full cycle from 0 to 2π, cos() and sin() together parameterize a unit circle: c(cos(theta), sin(theta)) is always a unit vector.

For circular data like wind direction or compass bearings, averaging angles directly produces wrong results — the average of 350° and 10° is not 0° but would be computed as 180°. The correct approach uses the mean of the cosine and sine components: atan2(mean(sin(theta)), mean(cos(theta))) gives the circular mean. This pattern requires both sin() and cos() together and is the foundation of circular statistics in R. The circular package extends these operations for more advanced directional data analysis and circular regression models that correctly account for the periodicity of angular and directional measurements.

In R, angles are always in radians unless you convert explicitly. The constant pi is built into R as a double-precision approximation of π. Use pi/180 to convert degrees to radians: cos(90 * pi/180) approximates cos(π/2).

cos() accepts values in radians. To convert degrees to radians before calling, multiply by pi / 180. The function is vectorized and returns NaN for NA inputs with a warning. cos(pi) returns -1 and cos(0) returns 1, consistent with the unit circle definition.

See also