# sqrt in r | Mastering the Square Root Function in R: A Comprehensive Guide

ASHISH GUPTA

4 min read

- R Programming
- R

**Introduction**

Are you want to find the square root of any number in R language When you are working with numerical data in R, one might common mathematical operation is getting the square root of any number it may be perfect square or not or even in float number we can find square root. In this blog post, we'll see how to use the "`sqrt()"`

function in R, tips to ensure you're using it effectively and also we see practical example of sqrt in R language..

**What is a Square Root?**

Before jumping into R, let's quickly recap or reminding you what a square root. Here is the little explanation of the square root of a number "`a"`

is a number "`b"`

such that a2=b let's take an example, the square root of 100 is 10, because 10^2=100.

it's quite simple.

`sqrt()`

** Function in R**

R has a built-in function called `sqrt()`

to calculation of the square root of a number.below code have the syntax of finding sqrt in R very simple to use:

```
a <- 36
sqrt(a)
```

Here, "`a"`

is the number to find the square root.

**Example:**

```
result <- sqrt(36)
print(result) # Output: 6
```

In above code, `sqrt(36)`

returns 6 because 4^2=16.

**Square Root of a Vector**

The `sqrt()`

function can also be applicable on vectors. Vector is **the most common data structure**. It is similar to an array of data factor, each of the same type (integer, double, character, logical, complex, and raw). This is useful when you are playing with integer datasets:

```
# let's implement a vector of having integer numbers
numbers <- c(9, 16, 25, 36, 49, 64, 81 ,100)
# Finding the square roots
sqrt_numbers <- sqrt(numbers)
print(sqrt_numbers) # Output: 2 3 4 5 6 7 8 9 10
```

**How to handle Negative Numbers**

Now let's find the square root of any negative number(negative number are those number which are less than 0) is not a real number. So by default it gives, the** **`sqrt()`

function in R returns `NaN`

(Not a Number) for negative inputs:

```
# let's try to find the square root of a negative number
negative_result <- sqrt(-6)
print(negative_result) # Output: NaN
```

To handle complex numbers, you need to use the `sqrt()`

function within the context of complex arithmetic:

```
complex_result <- sqrt(as.complex(-36))
print(complex_result) # Output: 0+6i
```

**What is complex number**

**Complex numbers** are those numbers that can be written in the form of **x+iy** where, x,y are real numbers and ‘i’ is an imaginary number is called “** iota”**. The value of i = (√-1). For example, 3+4i is a complex number, where 3 is a real number (Re) and 4i is an imaginary number (Im).

i = √-1

i^2 = -1

i^3 = -i

i^4 = i

**real Applications**

##### Calculating Standard Deviation

The standard deviation is the square root of the variance.a basic real application that use of the square root function is calculating the standard deviation of a given dataset and giving insight into the spread of data points.

```
# Creating a sample dataset
data <- c(10, 12, 23, 23, 16, 23, 21, 16)
# Calculating the variance
variance <- var(data)
# Calculating the standard deviation
std_dev <- sqrt(variance)
print(std_dev)
```

##### Pythagorean Theorem

The square root function is also useful in geometry. if you can use it to calculate the hypotenuse of a right-angled triangle using the Pythagorean theorem:

```
# Lengths of the other two sides
a <- 3
b <- 4
# Calculating the hypotenuse
hypotenuse <- sqrt(a^2 + b^2)
print(hypotenuse) # Output: 5
```

##### Scientific Research

For instance, the standard deviation, a critical measure of variability or dispersion in a dataset, is derived from the square root of variance:

```
# Sample dataset of heights in cm
heights <- c(165, 170, 175, 180, 185)
# Calculate the variance
variance <- var(heights)
# Standard deviation is the square root of variance
std_deviation <- sqrt(variance)
print(paste('Standard Deviation:', std_deviation))
```

##### Finance and Economics

One important application is in the assessment of investment risks and returns. The **Volatility** and non-volatility of a stock, which measures and calculate the standard deviation of its returns, highly depend on square root computations.

```
# Annual returns of a stock in percentage
returns <- c(-10, 5, 15, 20, -5)
# Calculate the variance of returns
variance <- var(returns)
# Volatility is the square root of variance
volatility <- sqrt(variance)
print(paste('Stock Volatility:', volatility))
```

**Conclusion**

In this blog we discuss what is square root, How to implement in r language or syntax of sqrt in R language.practical usecase like standard deviation ,Scientific Research , pythagorean theorem,Finance and Economics in R and etc. The `sqrt()`

in R is a very helpful for getting square root calculations, if you are in your project and deal with single integer numbers, vectors, or complex numbers etc.Do experiment with the `sqrt()`

function and apply it to different scenarios in your data analysis projects.

Happy coding!