Standard deviation in R: How to Use sd() in R

We have already seen how to calculate percentile and variance in R programming. We have besides seen how to calculate Mean and Mode in R. Let ’ s see how to compute the standard deviation, but before that, let ’ s understand what SD is .

What is Standard Deviation

The standard deviation of a population is the public square settle of the population variance. It is the measuring stick of the distribution of the values. The higher the standard deviation, the wide the spread of values. The lower the standard deviation, the close the banquet of values.

The standard deviation is a generally used mannequin of the degree of pas seul within a laid of data values. A gloomy standard deviation proportional to the bastardly prize of a sample distribution means the observations are tightly clustered ; larger values suggest observations are more unfold out .

standard deviation in r

To calculate the standard deviation in r, use the sd() function. The standard deviation of an observation variable in R is calculated by the square root of its variance .
The sd in R is a built-in routine that accepts the input object and computes the criterion deviation of the values provided in the object. The south dakota ( ) function accepts a numeric vector and legitimate arguments and returns the standard diversion .
If na.rm is TRUE, then missing values are removed before calculation proceeds. If the stimulation measure is a matrix or a data frame, a vector of the standard deviation of the column is returned .
The symbol for the population standard deviation is Σ (sigma). Its recipe is the be .
Calculate Standard Deviation in R using sd()

Syntax

The syntax of the south dakota ( ) function in R is the trace .

 south dakota ( x, na.rm = FALSE )

Parameters

x: It is a numeral vector or an gas constant object but not a factor coercible to numeric by as.double(x) .
na.rm: It is coherent. Should missing values be removed ?

Example

We will find the standard deviation of the Petal.length of the iris dataset .

 data ( iris )
 iris $ Petal.Length
 ln < - iris $ Petal.Length
 cat ( `` The standard deviation of iris petal length is : ``, `` \n '' )
 south dakota ( ln )

Output

[1] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 1.5 1.6 1.4 1.1 1.2 1.5 1.3 1.4
[19] 1.7 1.5 1.7 1.5 1.0 1.7 1.9 1.6 1.6 1.5 1.4 1.6 1.6 1.5 1.5 1.4 1.5 1.2
[37] 1.3 1.4 1.3 1.5 1.3 1.3 1.3 1.6 1.9 1.4 1.6 1.4 1.5 1.4 4.7 4.5 4.9 4.0
[55] 4.6 4.5 4.7 3.3 4.6 3.9 3.5 4.2 4.0 4.7 3.6 4.4 4.5 4.1 4.5 3.9 4.8 4.0
[73] 4.9 4.7 4.3 4.4 4.8 5.0 4.5 3.5 3.8 3.7 3.9 5.1 4.5 4.5 4.7 4.4 4.1 4.0
[91] 4.4 4.6 4.0 3.3 4.2 4.2 4.2 4.3 3.0 4.1 6.0 5.1 5.9 5.6 5.8 6.6 4.5 6.3
[109] 5.8 6.1 5.1 5.3 5.5 5.0 5.1 5.3 5.5 6.7 6.9 5.0 5.7 4.9 6.7 4.9 5.7 6.0
[127] 4.8 4.9 5.6 5.8 6.1 6.4 5.6 5.1 5.6 6.1 5.6 5.5 4.8 5.4 5.6 5.1 5.1 5.9
[145] 5.7 5.2 5.0 5.2 5.4 5.1

The standard deviation of iris petal length is:

[1] 1.765298

That is it. The standard deviation for the petal.length is 1.765298.
You can calculate the standard deviation without the south dakota ( ) function using the following code .

 sqrt ( union ( ( ln - hateful ( ln ) ) ^ 2 / ( length ( ln ) - 1 ) ) )

The accomplished code is following .

 data ( iris )
 iris $ Petal.Length
 ln < - iris $ Petal.Length
 big cat ( `` The standard deviation of iris petal length is : ``, `` \n '' )
 sqrt ( total ( ( ln - base ( ln ) ) ^ 2 / ( distance ( ln ) - 1 ) ) ) 

Output

[1] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 1.5 1.6 1.4 1.1 1.2 1.5 1.3 1.4
[19] 1.7 1.5 1.7 1.5 1.0 1.7 1.9 1.6 1.6 1.5 1.4 1.6 1.6 1.5 1.5 1.4 1.5 1.2
[37] 1.3 1.4 1.3 1.5 1.3 1.3 1.3 1.6 1.9 1.4 1.6 1.4 1.5 1.4 4.7 4.5 4.9 4.0
[55] 4.6 4.5 4.7 3.3 4.6 3.9 3.5 4.2 4.0 4.7 3.6 4.4 4.5 4.1 4.5 3.9 4.8 4.0
[73] 4.9 4.7 4.3 4.4 4.8 5.0 4.5 3.5 3.8 3.7 3.9 5.1 4.5 4.5 4.7 4.4 4.1 4.0
[91] 4.4 4.6 4.0 3.3 4.2 4.2 4.2 4.3 3.0 4.1 6.0 5.1 5.9 5.6 5.8 6.6 4.5 6.3
[109] 5.8 6.1 5.1 5.3 5.5 5.0 5.1 5.3 5.5 6.7 6.9 5.0 5.7 4.9 6.7 4.9 5.7 6.0
[127] 4.8 4.9 5.6 5.8 6.1 6.4 5.6 5.1 5.6 6.1 5.6 5.5 4.8 5.4 5.6 5.1 5.1 5.9
[145] 5.7 5.2 5.0 5.2 5.4 5.1

The standard deviation of iris petal length is:

[1] 1.765298

Calculate the Standard deviation of Vector in R

To calculate the standard deviation of the vector, use the south dakota ( ) function. To define a vector, use the c ( ) function and pass the elements as arguments. You can besides use the : ( colon ) hustler to create a vector .

 vec < - 1:5
 big cat ( `` The standard deviation of vector is '', `` \n '' )
 south dakota ( vec )

Output

The standard deviation of vector is
[1] 1.581139

And we get the standard deviation of the numeral vector, which in our case is 1.581139 .

Calculate standard deviation of Array in R

To calculate the standard deviation of an array in R, use the south dakota ( ) function. To create an array in R, use the array()  serve. The array()  officiate takes a vector as an argument and uses the dim parameter to create an array .

 recreational vehicle < - cytosine ( 19, 21 )
 rv2 < - coke ( 46, 4 )
 arr < - array ( coke ( recreational vehicle, rv2 ), dim = deoxycytidine monophosphate ( 2, 2, 2 ) )
 big cat ( `` The standard deviation of array is '', `` \n '' )
 south dakota ( arr )

Output

The standard deviation of array is
[1] 16.11565

Calculate the Standard deviation of data frame in R

To calculate the standard deviation of a datum frame in R, use the south dakota ( ) function. To create a datum frame in R, use data.frame ( ) routine. We will find the standard deviation of a numerical column of the datum frame .

 df < - data.frame ( service_id = c ( 1:5 ) ,
 service_name = vitamin c ( `` Netflix '', `` Disney+ '', `` HBOMAX '', `` Hulu '', `` Peacock '' ) ,
 service_price = cytosine ( 18, 10, 15, 7, 12 ) ,
 stringsAsFactors = FALSE )
 caterpillar ( `` The standard deviation of service_price is '', `` \n '' )
 south dakota ( df $ service_price )

Output

[1] 4.27785

And we get the SD of the data frame column .
That ’ s it for this tutorial .

More posts

pnorm in r
rnorm in radius
dnorm in radius
Krunal Lathiya Author Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In accession, Krunal has excellent cognition of Data Science and Machine Learning, and he is an technical in R Language. Krunal has written many programming blogs, which showcases his huge expertness in this field .

source : https://thefartiste.com
Category : Tech

About admin

I am the owner of the website thefartiste.com, my purpose is to bring all the most useful information to users.

Check Also

articlewriting1

Manage participants in a zoom meeting webinar

Call the people who attend the meet as follows Alternate host host Who scheduled the …

Leave a Reply

Your email address will not be published.