To **calculate** the **square root** of any given** number** in **R**, you can use the **“sqrt()”** function. For example, **sqrt(16)** returns **4**, and **sqrt(c(1, 4, 9))** returns **c(1, 2, 3)**.

**Syntax**

```
sqrt(num)
```

**Parameters**

**num:** It’s a numerical value greater than 0.

**Return value**

It returns the square root of the input.

**Example 1: Simple program to calculate square root in R**

```
num <- 16
cat("The square root of 16 is: ", sqrt(num))
```

**Output**

`The square root of 16 is: 4`

**Example 2: Applying sqrt() function to vector in R**

To find the square root of Vector in R, you can use the sqrt() function. The sqrt() function takes a Vector as an argument and returns each element’s square root.

```
rv <- c(11, 19, 21, 16, 49, 46)
rv_sqrt <- sqrt(rv)
print(rv_sqrt)
```

**Output**

`[1] 3.316625 4.358899 4.582576 4.000000 7.000000 6.782330`

You can see that it returns the square root of every vector element.

**Example 3: Applying sqrt() function to Matrix in R**

To find the square root of Matrix in R, use the **“sqrt()”** function. The **“sqrt()”** function takes Matrix as an argument and returns the square root of each element.

```
rv <- c(11, 19, 21, 16, 49, 46)
mtrx <- matrix(rv, nrow = 2, ncol = 3)
print(mtrx)
cat("After calculating square root of matrx", "\n")
mt_sqrt <- sqrt(mtrx)
print(mt_sqrt)
```

**Output**

```
[,1] [,2] [,3]
[1,] 11 21 49
[2,] 19 16 46
After calculating square root of matrx
[,1] [,2] [,3]
[1,] 3.316625 4.582576 7.00000
[2,] 4.358899 4.000000 6.78233
```

**Example 4: Applying sqrt() function to List in R**

We can not find the square root of the List in R, and if we try to find it, it will give us the following error.

**non-numeric argument to mathematical function**

See the following code.

```
litt <- list(11, 19, 21, 16, 49, 46)
print(litt)
cat("After calculating square root of list", "\n")
list_sqrt <- sqrt(litt)
print(list_sqrt)
```

**Output**

```
After calculating square root of list
Error in sqrt(litt) : non-numeric argument to mathematical function
Execution halted
```

This error occurs when we are trying to find the square root of a non-numeric value.

The list and character string contain non-numeric values, returning a **non-numeric argument to a mathematical function **error.

**Example 5: Applying sqrt() function to factor**

We can get the error with **Factor **as well. But that error is different than the above.

```
fact <- factor(10)
sqrt(fact)
```

**Output**

```
Error in Math.factor(fact) : ‘sqrt’ not meaningful for factors
Execution halted
```

To calculate the square root of the factor and fix the error, use **“as.numeric()” **and **“as.character()”** with **sqrt()** method.

```
fact <- factor(10)
print("The factor is: ")
print(fact)
sqrt_fact <- sqrt(as.numeric(as.character(fact)))
print("The square root of factor is: ", sqrt_fact)
print(sqrt_fact)
```

**Output**

```
[1] "The factor is: "
[1] 10
Levels: 10
[1] "The square root of factor is: "
[1] 3.162278
```

You can see that now we get the square root of factor 10, which is **3.162278**

That’s it.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.