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

sqrt() function in R Language is used to calculate the mathematical square-root of the value passed to it as argument.

Syntax: sqrt(x)

Parameter:
x: Any numerical value greater than 0

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R code to calculate square root of a number
  
x1 <- 16
x2 <- 18
  
# Using sqrt() Function
sqrt(x1)
sqrt(x2)

chevron_right


Output:



[1] 4
[1] 4.242641

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R code to calculate square root of a number
  
x1 <- 8.34526
x2 <- -18
  
# Using sqrt() Function
sqrt(x1)
sqrt(x2)

chevron_right


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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.