The R sqrt method is one of the Math functions, which is useful to find the square root for an individual number or an expression. Let us see how to use sqrt in the R Programming language with an example.
R sqrt syntax
The syntax of the sqrt in R Programming language is as shown below:
sqrt(numeric_Expression); //Return Type is Integer
Numeric_Expression: It can be a numeric value or a valid numerical expression for which you want to find the square root in R. For example, square root of 3 is 1.732
- If the numeric_Expression is a positive value, the sqrt function returns the square root of a given value.
- If the numeric_Expression is a negative value, the sqrt function return NaN.
- numeric_Expression is not a number (NaN), or Negative Infinity, then sqrt in R returns NaN.
- If the numeric_Expression is positive infinity, the sqrt function returns the result as Positive Infinity.
R sqrt example 1
In this R square root 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
# 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")
sqrt example 2
The sqrt in R allows you to find the square root of numeric values. In this R square root program, We are going to find the square roots of different data and display the output
# 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))
R sqrt example 3
In this R square root 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
# SQRT in R example ChickWeight Total.weight <- aggregate(ChickWeight$weight, by = list(ChickWeight$Diet), FUN = sum) Total.weight sqrt(Total.weight$x)
sqrt Function Example 4
The R sqrt function also allows you to find the square roots of column values. In this example, We are going to find the square root of all the records present in the [Standard Cost], and [Sales Amount] columns using the sqrt function. For this R Square root example, we use the below-shown CSV data. Refer to Read CSV Function article to understand the steps involved in importing the CSV file.
This square root function code will return the square root values of the Standard Cost and Sales Amount column
# SQRT in R example getwd() product <- read.csv("Product_Data.csv", TRUE, sep = ",") print(product) # Square Roots sqrt(product$StandardCost) sqrt(product$SalesAmount)