How to calculate the square root of a number in R

The sqrt() function in R is used to calculate the square root of a non-negative number.

Figure 1 shows the mathematical representation of the sqrt() function.

Figure 1: Mathematical representation of the sqrt() function

Syntax

sqrt(number)

Parameter

sqrt() requires a non-negative number as a parameter.

Return value

sqrt() returns the square root of the number sent as a parameter.

If a negative number is sent as a parameter, then sqrt() returns NaN, along with a warning message.

Example

#Perfect square root
a <- sqrt(4);
print(paste0("sqrt(4): ", a))
b <- sqrt(0);
print(paste0("sqrt(0): ", b))
#simple number
c <- sqrt(5);
print(paste0("sqrt(5): ", c))
d <- sqrt(4.5);
print(paste0("sqrt(4.5): ", d))
#negative number
e <- sqrt(-2);
print(paste0("sqrt(-2): ", e))

Free Resources