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.

### Explanation

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.

### 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

In this example the sqrt function is taking the square root of a data frame. Note that it is returning the square root of each of the values. You could get just the square roots of column A by using x$A as the argument.

### Applications of sqrt in R

Square roots have innumerable applications. You can easily use this function as part of a larger equation. One simple application of this function would be as part of solving a quadratic equation. If the data, you are analyzing involves physics you will be using this function. As you get into data analysis, you will probably find uses for square roots that you never thought of before.

The square root function is a handy and easy to use function that you will get lots of use out it. Because you can use complex values as arguments, its power in r programming is more than you would expect. It is a tool that you should learn to use well, because you will be using it everywhere.