EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials R Programming Tutorial Standard Deviation in R
Secondary Sidebar
R programming Tutorial
  • Advanced
    • Statistical Analysis with R
    • R String Functions
    • Data Exploration in R
    • R CSV Files
    • KNN Algorithm in R
    • Sorting in R
    • lm Function in R
    • Hierarchical Clustering in R
    • R Normal Distribution
    • Binomial Distribution in R
    • Decision Tree in R
    • GLM in R
    • Arima Model in R
    • Linear Model in R
    • Predict Function in R
    • Survival Analysis in R
    • Standard Deviation in R
    • Statistical Analysis in R
    • Predictive Analysis?in R
    • T-test in R
    • Database in R
  • Basic
    • What is R Programming Language
    • Careers in R Programming
    • Install R
    • List of R Packages
    • Introduction of R Tools Technology
    • R Programming Language
    • DataSet in R
    • What is RStudio?
    • R-studio-Functions
    • R Packages
    • Time series?in R
    • R Data Types
    • R for data science
    • R Operators
    • R Data Frame
    • R Analytics Tool
    • R Tree Package
    • Vectors in R
  • Control statement
    • If Statement in R
    • If Else Statement in R
    • Else if in R
    • Switch Statement in R
  • Loops
    • Loops in R
    • For Loop in R
    • Nested For Loop in R
    • While Loop in R
    • Next in R
  • Chart/graphs
    • Graphs in R
    • Bar Charts in R
    • Pie Chart in R
    • Histogram in R
    • Line Graph in R
    • Plot Function in R
    • Scatterplot in R
    • R Boxplot labels
  • Regression in R
    • Simple Linear Regression in R
    • Linear Regression in R
    • Multiple Linear Regression in R
    • Logistic Regression in R
    • Poisson Regression in R
    • OLS Regression in R
    • P-Value in Regression
  • Anova in R
    • ANOVA in R
    • One Way ANOVA in R
    • Two Way ANOVA in R
  • Data Structure
    • R list
    • Arrays in R
    • Data Frames in R
    • Factors in R
    • R Vectors
  • Programs
    • Functions in R
    • Boxplot in R
    • R Program Functions
    • Factorial in R
    • Random Number Generator in R
  • Interview question
    • R Interview Questions

Related Courses

R Programming Certification Course

Statistical Analysis Course Training

All in One Data Science Courses

Standard Deviation in R

By Priya PedamkarPriya Pedamkar

Standard Deviation in R

Overview Of Standard Deviation in R

Standard deviation in R is a statistic that measures the amount of dispersion or variation of a set of value, generally, it is used when we are dealing with values where we have to find the difference between the values and the mean.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,171 ratings)

Mathematical formula of standard deviation:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Standard Deviation in R

Where,

  • S: Sample standard deviation.
  • N: Number of observations.
  • xi: Observed value of the sample item.
  • x̅: Mean value of the observation.

How to Calculate Standard Deviation?

Steps to calculate Standard deviation are:

  • Step 1: Calculate the mean of all the observations.
  • Step 2: Then for each observation, subtract the mean and double the value of it (Square it).
  • Step 3: We got some values after deducting mean from the observation, do the summation of all of them.
  • Step 4: Lastly, divide the summation with the number of observations minus 1.

You will get the standard deviation as a result after completing 4 steps.

Examples with Steps of Standard Deviation

Let’s take an example and follow these steps.

Example #1

Data set looks like,

4,8,9,4,7,5,2,3,6,8,1,8,2,6,9,4,7,4,8,2

Step 1: Calculate the mean of all the observations,

  • Mean = (4 + 8 + 9 + 4 + 7 + 5 + 2 + 3 + 6 + 8 + 1 + 8 + 2 + 6 + 9 + 4 + 7 + 4 + 8 + 2) / 20
  • Mean = 107/20
  • Mean = 5.35

Step 2: For each observation, subtract the mean, we will put it in the tabular form for the convenience,

Standard Deviation in R-.1.1

Double the value of the column second (Observation – Mean)^2.

Standard Deviation in R-.1.2

Step 3: Summation of all the values present in the above column.

1.8225 + 7.0225 + 13.3225 + 1.8225 + 2.7225 + 0.1225 + 11.2225 + 5.5225 + 0.4225 + 7.0225 + 18.9225 + 7.0225 + 11.2225 + 0.4225+13.3225 + 1.8225 + 2.7225 + 1.8225 + 7.0225 + 11.2225 = 126.55

Step 4: We will calculate the Standard deviation, by dividing summation with the number of observations minus 1 and we will square root the result.

Standard Deviation = (126.55/19)^0.5 = 2.58079

Example #2

Now we will look into some other examples with different datasets. In this example, we have two columns. In one column there are some alphabetic codes which we assigned to the people and in the next column, we have the age of those sets of people.

Standard Deviation in R-.1.3

Step 1: We will upload the excel file in R. Here we will use read.csv function because our excel file is in csv format. Suppose this table is in excel, so how this will work in Rstudio, we will discuss this step by step. The name of the excel file is “alphabetic code”.

The function for the same looks like,

SD_age = read.csv("alphabetic code.csv")

Step 2: calculating the standard deviation from the excel file. As we can see, that 2 column contains a numeric value. We will run our code on that column specifically,

In R, the syntax for Standard Deviation looks like this:

standard_deviation_age = sd(SD_age)
standard_deviation_age

The output of the codes provides us the Standard deviation of the dataset. The standard deviation of the Age is 15.52926.

Methods of Standard Deviation in R

There are multiple methods to calculate Standard deviation in R. We will here discuss one long method and one very short method.

1. Long Method

This method will incorporate the same steps which we did earlier in this article, the only difference now is we will use R commands.

Step 1: Calculate the mean of all the observations.

Code:

dataset = c(4,8,9,4,7,5,2,3,6,8,1,8,2,6,9,4,7,4,8,2)
meandataset = mean(dataset)
meandataset

Output:

Observation - Mean

Step 2: For each observation, subtract the mean from all the observations of the dataset. For this, we will make a function in R, which will help us to find [Observation-Mean].

Code:

dataset = c(4,8,9,4,7,5,2,3,6,8,1,8,2,6,9,4,7,4,8,2)
meandataset = mean(dataset)
sumdataset = function(dataset){dataset-meandataset}
Observation_Mean = sumdataset(dataset)
Observation_Mean

Output:

Standard Deviation in R eg2

The output shows Observation – Mean for all the values in our dataset. Now we will square each value of this output an do the summation.

Code:

dataset = c(4,8,9,4,7,5,2,3,6,8,1,8,2,6,9,4,7,4,8,2)>
meandataset = mean(dataset)>
sumdataset = function(dataset){dataset-meandataset}
Observation_Mean = sumdataset(dataset)
square_Observation_Mean = Observation_Mean*Observation_Mean
square_Observation_Mean

Output:

Mean

Step 3: Summation of all the values present in the above column. Now we will add all these [(Observation – Mean)^2].

Code:

dataset = c(4,8,9,4,7,5,2,3,6,8,1,8,2,6,9,4,7,4,8,2)
meandataset = mean(dataset)
sumdataset = function(dataset){dataset-meandataset}
Observation_Mean = sumdataset(dataset)
square_Observation_Mean = Observation_Mean*Observation_Mean
sum_square_Observation_Mean = sum(square_Observation_Mean)
sum_square_Observation_Mean

Output:

Output

Step 4: We will calculate the Standard deviation. No, we will put all the necessary information which we derive in all the above steps into this function:

Standard Deviation

  • Standard Deviation = (126.55/19)^0.5
  • Standard Deviation = 2.58079

In R, the syntax for Standard Deviation looks like this:

Code:

dataset = c(4,8,9,4,7,5,2,3,6,8,1,8,2,6,9,4,7,4,8,2)
meandataset = mean(dataset)
sumdataset = function(dataset){dataset-meandataset}
Observation_Mean = sumdataset(dataset)
square_Observation_Mean = Observation_Mean*Observation_Mean
sum_square_Observation_Mean = sum(square_Observation_Mean)
standard_deviation = sqrt(sum_square_Observation_Mean/19)
standard_deviation

Output:

Long method output

Hence we can see the Standard deviation is the same which we got earlier.

2. Short Method

Syntax in R for the direct method looks like,

sd(x, na.rm = FALSE)

Where sd is Standard deviation. x is those set values for which we need to find the standard deviation. na.rm, if it is true then it will remove all the missing value from the dataset/ matrix /data frames etc. And if it is false, then it won’t remove missing value from the data set.

Code:

dataset = c(4,8,9,4,7,5,2,3,6,8,1,8,2,6,9,4,7,4,8,2)
sd(dataset)

Output:

short method output

Conclusion

Standard deviation tells us how much our observations in the datasets are spread out from the actual mean. Significance of low and high standard deviation is:

  • High Standard deviation tells us that the numbers/observations in the dataset are more spread out.
  • Low Standard deviation tells us that the numbers/observations in the dataset are less spread out or we can say that they are close to mean.

Recommended Articles

This is a guide to Standard Deviation in R. Here we discuss the steps and methods of Standard Deviation in R along with examples and code implementation. You may also have a look at the following articles to learn more –

  1. Syntax of the Linear Model in R
  2. Advantages of Simple Linear Regression in R
  3. 4 Main Sections of for Loop in PowerShell
  4. How to Create a Factor in R?
Popular Course in this category
R Programming Training (13 Courses, 20+ Projects)
  13 Online Courses |  20 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Statistical Analysis Training (15 Courses, 10+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more