R Tutorial R Charts & Graphs R Statistics R References

R - sqrt() Function



The R sqrt() function returns the square root of the given number. In special cases it returns the following:

  • If the argument is NaN or less than zero, then the result is NaN.
  • If the argument is positive infinity, then the result is positive infinity.
  • If the argument is zero, then the result is the same as the argument.

Syntax

sqrt(x)

Parameters

x Required. Specify column to compute on.

Return Value

Returns the square root of the specified number.

Example:

In the example below, sqrt() function is used to find out the square root of the given number.

#operating on single element atomic vector
print(sqrt(9))
print(sqrt(16))
print(sqrt(25))

cat("\nOperating on vector\n")
#operating on vector
v <- c(4, 25, 64)
print(sqrt(v))

cat("\nOperating on matrix\n")
#operating on matrix
m <- matrix(c(10, 50, 100, 225, Inf, NaN), nrow=2)
print(sqrt(m))

cat("\nOperating on first column of matrix\n")
#operating on first column of matrix
print(sqrt(m[,1]))

The output of the above code will be:

[1] 3
[1] 4
[1] 5

Operating on vector
[1] 2 5 8

Operating on matrix
         [,1] [,2] [,3]
[1,] 3.162278   10  Inf
[2,] 7.071068   15  NaN

Operating on first column of matrix
[1] 3.162278 7.071068

❮ R Math Functions