Compute Variance and Standard Deviation of a value in R Programming – var() and sd() Function

var() function in R Language computes the sample variance of a vector. It is the measure of how much value is away from the mean value.

Syntax: var(x)

Parameters:
x : numeric vector

Example 1: Computing variance of a vector

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# variance of vector
  
# Create example vector
x <- c(1, 2, 3, 4, 5, 6, 7)   
  
# Apply var function in R 
var(x)                       
  
print(x)

chevron_right


Output:



 4.667

Here in the above code, we took an example vector “x1” and calculated its variance.

sd() Function

sd() function is used to compute the standard deviation of given values in R. It is the square root of its variance.

Syntax: sd(x)

Parameters:
x: numeric vector

Example 1: Computing standard deviation of a vector

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# standard deviation of vector
  
# Create example vector
x2 <- c(1, 2, 3, 4, 5, 6, 7
  
# Compare with sd function
sd(x2)                          
  
print(x2)

chevron_right


Output: 2.200

Here in the above code, we took an example vector “x2” and calculated its standard deviation.

My Personal Notes arrow_drop_up

Recommended Posts:


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.