sin()
sin(x) The sin() function computes the trigonometric sine of numeric values in R. It operates element-wise on vectors and accepts angles in radians.
Syntax
sin(x)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | numeric | — | Numeric vector or single value representing an angle in radians |
Examples
The examples below start with the simplest case — computing sin() for a single angle — then show how vectorization works across multiple values, how to convert from degrees to radians, and how sin() combines with cos() for circular geometry and periodic waveform generation.
Basic usage
# Compute sine for a single angle (pi/4 = 45 degrees)
sin(pi / 4)
# [1] 0.7071068
sin(0)
# [1] 0
sin(pi / 2)
# [1] 1
Working with vectors
Since sin() is vectorized, you can pass a numeric vector of angles and get back a vector of sines — no loop or sapply() needed. This makes it straightforward to compute the sine at equally spaced points across an interval, which is how you build sine wave data for plotting or signal processing.
# Compute sine for multiple angles
angles <- c(0, pi/6, pi/4, pi/3, pi/2)
sin(angles)
# [1] 0.0000000 0.5000000 0.7071068 0.8660254 1.0000000
Converting degrees to radians
Most real-world angles come in degrees, but sin() expects radians. The conversion is deg * pi / 180. Wrapping this in a small helper function keeps your trig code readable and avoids repeating the conversion factor — which is easy to mistype — every time you call sin().
# Convert degrees to radians before using trig functions
deg_to_rad <- function(deg) deg * pi / 180
sin(deg_to_rad(30))
# [1] 0.5
sin(deg_to_rad(90))
# [1] 1
Common patterns
Creating periodic data
The most common use of sin() in R is generating smooth periodic signals. By creating a sequence of angles that spans a full period — 0 to 2 * pi — and passing it to sin(), you produce a complete sine wave that can serve as a synthetic test signal or a visual illustration of the function’s shape.
# Generate one period of sine wave
x <- seq(0, 2 * pi, length.out = 100)
y <- sin(x)
plot(x, y, type = "l")
Circular coordinates
When you need points on a unit circle, sin() and cos() work as a pair. Given a vector of angles theta, the x-coordinate is cos(theta) and the y-coordinate is sin(theta). This pairing appears in circular statistics, spatial data, and any domain where direction matters more than linear position.
# Calculate 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))
sin() in data analysis
sin() accepts angles in radians. To convert degrees to radians: multiply by pi / 180. The inverse function is asin(), which returns values in the range [-π/2, π/2].
In data analysis, sin() and cos() together are used to represent periodic or cyclical patterns. For time series with daily or weekly seasonality, encoding the time index as both sine and cosine features (with appropriate period) allows linear models to capture cyclical effects without categorical day-of-week variables. This is called circular feature encoding.
For example, for daily seasonality with a 24-hour period: sin(2 * pi * hour / 24) and cos(2 * pi * hour / 24) create two features that together represent the time of day as a position on a circle, with midnight and the next midnight mapping to the same point.
sin(0) is exactly 0, and sin(pi) is approximately zero but not exactly (about 1.22e-16). For trigonometric computations that should produce exact values at the cardinal angles, compare with a small tolerance using abs(sin(x)) < 1e-10 rather than exact equality. For converting back from a sine value to an angle, use asin() which returns angles in radians in the range [-π/2, π/2].
Fourier decomposition, which underlies spectral analysis and signal processing, expresses periodic signals as sums of sine and cosine terms. The fft() function in base R computes the discrete Fourier transform; sin() and cos() are used when constructing explicit Fourier basis functions or plotting the components of a decomposed signal. For audio, vibration, and seasonal time-series analysis, understanding the relationship between sin(), cos(), and fft() is foundational.
sin() returns values in the range [-1, 1]. Periodicity means sin(x + 2 * pi) equals sin(x) for any x, but floating-point arithmetic means these will not be exactly equal — sin(2 * pi) is approximately -2.4e-16 rather than exactly 0. For exact zero comparisons, use all.equal() rather than ==. The sinpi(x) function, which computes sin(pi * x), avoids this issue by using a specialized algorithm that returns exact results at integer multiples.