EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials R Programming Tutorial Standard Deviation in R
 

Standard Deviation in R

Priya Pedamkar
Article byPriya Pedamkar

Updated June 23, 2023

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 values; generally, it is used when dealing with values where we have to find the difference between the values and the mean.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Mathematical formula of standard deviation:

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?

The 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 the mean from the observation and do the summation of all of them.
  • Step 4: Divide the summation with the number of words 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

The data set looks like this,

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 tabular form for 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 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 by 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, we assigned some alphabetic codes to the people; 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 the 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 this,

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

Step 2: Calculate 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 with the Standard deviation of the dataset. The standard deviation of the age is 15.52926.

Methods of Standard Deviation in R

Multiple methods exist to calculate Standard deviation in R. We will discuss one long and very short process.

1. Long Method

This method will incorporate the same steps 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 and 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 all the values present in the above column. 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 as we got earlier.

2. Short Method

Syntax in R for the direct method looks like this,

sd(x, na.rm = FALSE)

Where sd is the Standard deviation. x is those set values for which we need to find the standard deviation. na.rm, if it is true, it will remove all the missing values from the dataset/ matrix /data frames, etc. And if it is false, then it won’t remove the 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. The significance of low and high standard deviation is:

  • A high Standard deviation tells us that the numbers/observations in the dataset are more spread out.
  • A 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 the 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?

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

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

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

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

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW