The R sqrt() is an inbuilt Math function used to calculate the square root for an individual number or an expression.

**Square root in R**

To find the square root in R, use the **sqrt() **function. The **sqrt()** method in R calculates the square root of a numerical object.

**Syntax**

```
sqrt(n)
```

**Parameters**

The sqrt() function accepts only one parameter, which takes **n **as a number.

**Return Value**

It returns the square root of the input.

**Example**

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

**Output**

`The square root of 16 is: 4`

**Applying sqrt() function to Vector in R**

To find the square root of Vector in R, 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 element of the vector.

**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
```

**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, then 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, and that is why it returns a **non-numeric argument to mathematical function **error.

**How to calculate the square root of Factor in R**

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 resolve 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**

**Conclusion**

R Language provides mathematical function sqrt() that calculates the square root of numerical objects. It won’t work on the list or character string data type.

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.