# sqrt in r: How To Calculate Square Root in R

Whenever you are dealing with numbers eventually you are going to need to find the square root of a value. Because of this fact, every programming language has a square root function and r is no different.

### Description (square root function in R)

If you are needing to do a square root the function is sqrt in r. It has the format of sqrt(x) where “x” is the values whose square roots are being calculated. This formula will find a positive square root when dealing with either an integer or double-precision numeric value. However, it can also produce square roots for complex values. The argument for this function can be a single value variable, a vector, a data frame, or even a single column of a data frame.

### Functionality and Limitations

The computation of square roots in R is efficient and versatile, working across multiple numeric types—whether integer, floating-point, or complex numbers. An attempt to compute the square root of non-numeric inputs results in an error. For negative inputs, the function yields a NaN (Not a Number) with an accompanying warning message. To mitigate this, you can use the abs() function which transforms negative inputs to positive before taking the square root, thereby avoiding the warning.

The square root function works with integer values, double precision values and complex values. If you enter a non numeric argument you will get an error message. If you enter a negative integer, or double precision argument you will get a warning message along with a return value of NaN. Each square root is rounded off to a rational number with six digits after the decimal point. One way to avoid a warning message with a negative value is to use the absolute value function to convert it into a positive number.

Handling Negative Inputs:

### Examples of using sqrt in R

Here are five r code examples of the square root function in action.

> x = 16
> sqrt(x)
[1] 4

This is the simplest possible example of the square root function. Here it is simply taking the square root of a single number.

> x = c(1, 4, 9, 16, 25)
> x
[1] 1 4 9 16 25
> sqrt(x)
[1] 1 2 3 4 5

This example of the square root function is taking the square root of a numeric vector. Note that it supplies the square root of each of the values within the vector.

> x = complex(real = 5,imaginary = 7)
> x
[1] 5+7i
> sqrt(x)
[1] 2.607904+1.342074i

This example of the square root function is taking the square root of a complex value.

> x = complex(real = -1,imaginary = 0)
> x
[1] -1+0i
> sqrt(x)
[1] 0+1i

This example of the square root function is taking the square root of -1 as a complex value. Note that it is returning the imaginary number i also in the form of a complex number.

> x = data.frame(A = c(1:5), B = c(6:10))
> x
A B
1 1 6
2 2 7
3 3 8
4 4 9
5 5 10
> sqrt(x)
A B
1 1.000000 2.449490
2 1.414214 2.645751
3 1.732051 2.828427
4 2.000000 3.000000
5 2.236068 3.162278

### Utilizing Power Functions for Square Root Computations

The power function in R, denoted as ^ or **, computes square roots by raising the number to the power of 0.5. It’s equivalent to the sqrt() function.

### Square Root of a Squared Variable Method

To find the square root of a squared variable in R, apply the sqrt() function or raise the variable to the power of 0.5. This will revert the squared value to its base value, provided it’s non-negative.

Scroll to top