The R sqrt method is one of the R Math function, which is used to find the square root of an individual number, or an expression. In this article we will show you, How to use sqrt in R Programming language with example.

## R sqrt syntax

The basic syntax of the sqrt in R Programming language is as shown below:

1 |
sqrt(<em>numeric_Expression</em>); //Return Type is Integer |

** Numeric_Expression:** It can be a numeric value or a valid numerical expression for which you want to find square root.

- If the
*numeric_Expression*is positive value, sqrt function will return the square root of a given value. - If the
*numeric_Expression*is negative value, sqrt function will return.**NaN** - If the
*numeric_Expression*is not a number (**NaN**), or Negative Infinity then sqrt in R will return.**NaN** - If the
*numeric_Expression*is positive infinity, sqrt function will return the result as**Positive Infinity**

## R sqrt example 1

In this program, We are going to find the square roots of Positive and negative Zeros, Positive and Negative Infinity, +ve and -ve Not a Number values, and the string data

**R CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# SQRT in R example # Square Root of both Positive and negative zeros sqrt(0) sqrt(-0) # Square Root of Not a Number sqrt(NaN) sqrt(-NaN) # Square Root of +ve and -ve Infinity sqrt(Inf) sqrt(-Inf) # Square Root of Characters sqrt("Tutorial Gateway") |

**OUTPUT**

## R sqrt example 2

The sqrt in R allows you to find the square root of a numeric values. In this program, We are going to find the square roots of different data and display the output

**R CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# SQRT in R example # Square Root of Positive values sqrt(64) sqrt(25.659) # Square Root of Negative values sqrt(-10.0897) # Absolute function will convert the Negative value to Positive # Next, sqrt will find the square root of 35.659 sqrt(abs(-35.659)) # Square Root on vectors num <- c(-25.526, 256.32, -36.5, -81 , -525.796) sqrt(num) sqrt(abs(num)) |

**TIP:** Please refer R abs Function to understand the functionality of abs

**OUTPUT**

## R sqrt example 3

In this program, We are going to find the absolute values of List data and display the output. For this example, we are using the * ChickWeight* data set provided by R

**R CODE**

1 2 3 4 5 6 7 8 |
# SQRT in R example ChickWeight Total.weight <- aggregate(ChickWeight$weight, by = list(ChickWeight$Diet), FUN = sum) Total.weight sqrt(Total.weight$x) |

**OUTPUT**

## R sqrt Function Example 4

The sqrt in R also allows you to find the square roots of a column values. In this example, We are going to find the square root of all the records present in [Standard Cost], and [Sales Amount] columns using sqrt Function.

For this, We are going to use the below shown csv data, and I suggest you to refer, R Read CSV Function article to understand the steps involved in importing csv file.

**R CODE**

1 2 3 4 5 6 7 8 9 10 |
# SQRT in R example getwd() product <- read.csv("Product_Data.csv", TRUE, sep = ",") print(product) # Square Roots sqrt(product$StandardCost) sqrt(product$SalesAmount) |

**OUTPUT**

From the above screenshot you can observe that, sqrt function is returning the square root values of the Standard Cost, and Sales Amount column

Thank You for Visiting Our Blog

## Leave a Reply