Open In App

Calculate Square root of a number in R Language - sqrt() Function

Last Updated : 28 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Square Root of a number is the value that yields the original number when multiplied by itself. If "x" is a number, its square root is written in mathematical notation as √x. we can calculate the square root in R using the sqrt() function.

In the R Programming Language, we have an inbuilt function known as the sqrt() Function to calculate the square root in R.

sqrt() function in R

To find a number's square root in R, use the sqrt() function.

  • The integer you wish to find the square root of is the only argument required.
  • The function yields the supplied number's positive square root.

Syntax: sqrt(x)

Parameter:

x: Any numerical value greater than 0

Calculate Square root using sqrt() Function

R
# R code to calculate square root of a number 

x1 <- 16
x2 <- 18

# Using sqrt() Function 
sqrt(x1) 
sqrt(x2) 

Output:

[1] 4
[1] 4.242641

Calculate Square root of a decimal number using sqrt() Function

R
# R code to calculate square root of a number 

x1 <- 8.34526
x2 <- -18

# Using sqrt() Function 
sqrt(x1) 
sqrt(x2) 

Output:

[1] 2.888816
[1] NaN
Warning message:
In sqrt(x2) : NaNs produced

Here, in the above code, NaN is produced because we passed a negative value in the sqrt() function.

Calculate Square Roots of Multiple Numbers in a Vector

R
# Calculate square roots of multiple numbers in a vector
numbers <- c(9, 25, 49, 64)
results <- sqrt(numbers)
print(results)  

Output:

[1] 3 5 7 8

we calculate the square roots of multiple numbers stored in a vector. The sqrt() function is applied to each element in the vector, and the results are stored in another vector called results.


Next Article
Article Tags :

Similar Reads