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
x1 <- 16
x2 <- 18
sqrt (x1)
sqrt (x2)
|
Output:
[1] 4
[1] 4.242641
Calculate Square root of a decimal number using sqrt() Function
R
x1 <- 8.34526
x2 <- -18
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
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
.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Nov, 2023
Like Article
Save Article