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 an example.
R sqrt syntax
The basic 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.
- If the numeric_Expression is a positive value, sqrt function will return the square root of a given value.
- If the numeric_Expression is a negative value, sqrt function will return NaN.
- 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
# 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")
R sqrt example 2
The sqrt in R allows you to find the square root of numeric values. In this program, We are going to find the square roots of different data and display the output
# 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
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)
R sqrt Function Example 4
The sqrt in R 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 [Standard Cost], and [Sales Amount] columns using sqrt Function.
For this R Square root example, We are going to use the below-shown csv data, and I suggest you refer, R Read CSV Function article to understand the steps involved in importing CSV file.
# SQRT in R example getwd() product <- read.csv("Product_Data.csv", TRUE, sep = ",") print(product) # Square Roots sqrt(product$StandardCost) sqrt(product$SalesAmount)
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