rguides

sqrt()

sqrt(x)

The sqrt() function computes the square root of numeric values in R. It operates element-wise on vectors.

Syntax

sqrt(x)

Parameters

ParameterTypeDefaultDescription
xnumericA numeric vector or scalar

Examples

The examples build from basic scalar computation through vectorized operation, handling of edge cases like negative inputs, and practical applications in statistics and geometry. Each section shows both the sqrt() call and the mathematical context that motivates it.

Basic usage

# Square root of a single value
sqrt(16)
# [1] 4

sqrt(0)
# [1] 0

sqrt(2)
# [1] 1.414214

Working with vectors

sqrt() inherits R’s vectorized semantics: passing a vector of any length returns a vector of the same length with each element replaced by its square root. This applies to both dense sequences like 1:10 and explicitly constructed vectors. No explicit loop or apply function is needed for element-wise computation.

# Square root of a vector
sqrt(c(4, 9, 16, 25))
# [1] 2 3 4 5

sqrt(1:10)
# [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 3.000000 3.162278

Handling negative values

In the real numbers, square roots of negative values are undefined, and sqrt() signals this by returning NaN with a warning. If your computation needs complex results — for example, when solving polynomial equations where complex roots are expected — convert the input to complex first with as.complex() or by adding 0i. The complex-aware sqrt() then returns a proper complex number.

# Square root of negative values returns NaN
sqrt(-1)
# [1] NaN

# Use complex() for complex square roots
sqrt(-1 + 0i)
# [1] 0+1i

Common patterns

Standard deviation calculation

The standard deviation is the square root of the variance, and sqrt() is the last step in computing it by hand. While sd() does this in one call, writing out sqrt(mean((x - mean(x))^2)) makes the relationship between variance and standard deviation explicit — each term in the expression corresponds directly to a step in the definition.

# Population standard deviation
x <- c(10, 12, 14, 16, 18)
sqrt(mean((x - mean(x))^2))
# [1] 2.828427

Distance calculations

Euclidean distance in any number of dimensions follows the same pattern: square the differences, sum them, take the square root. The two-dimensional case is the familiar Pythagorean formula, and sqrt() is the final operation that converts squared distance back to the same units as the original coordinates.

# Euclidean distance in 2D
x1 <- 0; y1 <- 0
x2 <- 3; y2 <- 4
sqrt((x2 - x1)^2 + (y2 - y1)^2)
# [1] 5

How sqrt() behaves and when to use it

sqrt(x) is equivalent to x^0.5 but runs as a compiled C primitive, so it beats the exponentiation operator for large vectors. Prefer sqrt() in numerical code where the intent is clearly “square root.”

For negative inputs, sqrt() returns NaN with a warning. If you need complex square roots, convert to a complex number first: sqrt(-1 + 0i) returns 0+1i.

sqrt() is frequently used in statistical calculations. Standard deviation is the square root of variance, Euclidean distance is the square root of the sum of squared differences, and the root-mean-square error (RMSE) uses sqrt() to bring a metric back to the original scale of the data:

# Root mean square error
actual <- c(3, 5, 7, 9)
predicted <- c(2.8, 5.2, 6.8, 9.1)
rmse <- sqrt(mean((actual - predicted)^2))

When plotting data, sqrt() is sometimes used as a mild variance-stabilizing transformation for count data. It compresses large values less aggressively than log(), which makes it useful when your data contains zeros that you cannot log-transform.

sqrt() propagates NA values without error — if any element of the input vector is NA, the corresponding output is also NA. This is consistent with how most vectorized math functions work in R. Use na.rm in aggregation functions before passing to sqrt() if you want to ignore missing values.

For very large vectors, sqrt() runs in a tight C loop and allocates a single output vector. It does not create intermediate copies, so memory usage is predictable: one numeric vector in, one numeric vector out. This makes it safe to use on vectors with millions of elements without concern for memory pressure.

When working with integer inputs, sqrt() always returns a double, even when the result is a whole number. sqrt(4L) returns 2 (type double), not 2L. This is rarely a problem in practice, but worth knowing if you are passing the result to a function that checks storage type explicitly.

sqrt() returns NaN for negative inputs with a warning. For complex square roots, convert first: sqrt(as.complex(-1)) returns 0+1i. The function is vectorized and NA-propagating. sqrt(x) is equivalent to x^0.5 but is computed via a dedicated hardware instruction and is faster for large vectors.

See also