Sign In
Sign In

How to Find Standard Deviation in R

How to Find Standard Deviation in R
Umair Khurshid
Technical writer
R
30.01.2025
Reading time: 7 min

Standard deviation is a statistical technique, which shows to what extent the values ​​of the studied feature deviate on average from the mean.  We use it to determine whether the units in our sample or population are similar with respect to the studied feature, or whether they differ significantly from each other. If you want to learn how to find standard deviation in R or just learn what standard deviation is, then read on.

This guide will offer a detailed explanation of calculating standard deviation in R, covering various methods and practical examples to assist users in analyzing data efficiently.

The Mathematics Behind Standard Deviation

Standard deviation is a measure defining the average variation of individual values ​​of a statistical feature from the arithmetic mean. It has a more intuitive interpretation as a measure of variability of a distribution. If the calculation had been undertaken with distances from the mean, then the sum would always be 0, a very shaky result. 

The mathematical formula of standard deviation is:

=∑(xi​−μ)2N​

Where Σ represents sum, xi​ is each observation, μ is the mean of the data, and N is the total number of observations. It is usually abbreviated as SD. 

The smaller the standard deviation, the closer the values are to the average, which shows that the data is more consistent. To properly judge whether the SD is small or large, it's important to know the range of the scale being used.

The Significance of Standard Deviation

The standard deviation is very helpful when comparing the variability between two data sets of similar size and average. Only using the simple average often does not help in deeper analysis. What good is it that we know the average salary in the company, if one does not know the variability of the salary? Do all employees get exactly the same? Or maybe the manager is overstating the average salary? To dig deeper and get to the underlying truth, we will have to calculate standard deviation.

Similarly, standard deviation is also helpful to find the risk while making investment decisions. If on the stock exchange, one company brought an average annual profit of 4% and another an average annual profit of 5%, it does not mean that it is better to choose the second company without thinking. 

Setting aside both fundamental and technical analysis of a specific company, as well as the broader macroeconomic conditions, it's valuable to focus on the fluctuations in the quotations themselves.

If the stock value of the first company had slight, several percent fluctuations during the year, and the other fluctuated by several dozen percent. Then it is logical that the investment in the first company was much less risky. And to compare different rates of return and check their riskiness, you can use the standard deviation.

Different Ways to Find Standard Deviation in R

To perform any kind of analysis, first we must have some data. In R you can input manually by defining a vector or importing it from external sources, such as excel or CSV file. Let’s create a vector with six values:

data <- c(4, 8, 6, 5, 3, 7)

Alternatively, datasets can be imported using the read.csv() function, which loads data from a CSV file into R. Here's an example of importing data:

# Read a CSV file into a data frame
data <-read.csv("datafile.csv")
# Install the 'readxl' package install.packages("readxl")
# Load the library library(readxl)
# Read an Excel file into a data frame data_excel <- read_excel("datafile.xlsx", sheet = 1)

Finding Sample Standard Deviation in R

A quick and easy way to standard deviation of a sample is through the sd() function which is one of the built-in function in R. It takes a data sample, often in the form of a vector, as input and returns the standard deviation. For example, to measure the SD of the vector created earlier:

sd(data)

Output:

[1] 1.870829

If your sample has missing or null values, then you just need to set the parameter na.rm=TRUE in the sd() function and the missing value will not be included in the analysis:

standard_deviation <- sd(data, na.rm = TRUE)

Finding Population Standard Deviation in R

To calculate the population standard deviation, we will first find the mean and subtract it from each observation in the dataset and square the results. Once we have the squared differences, we just have to find their average to find the variance. Finally, taking the square root of the variance will give us the population SD.

Here is the R code to manually compute population standard deviation:

mean_data <- mean(data)
squared_differences <- (data - mean_data)^2
mean_squared_diff <- mean(squared_differences)
standard_deviation_manual <- sqrt(mean_squared_diff)
print(standard_deviation_manual)

Grouped Standard Deviation in R

Let's say you are analyzing the grades of students across different subjects in a school. The categorical variable here is “subject,” and you want to know not only the average grade for each subject but also the variation in grades. This will help us understand if certain subject have a wide or uniform range of grades.

To determine the standard deviation for each category in a dataset containing categorical variables, one can utilize the dplyr package. The group_by() function facilitates the segmentation of the data by the categorical variable, and summarise() then calculates the SD for each distinct group.

Before moving to calculation, we will install the dplyr package:

install.packages("dplyr")

Following our earlier example, let’s take a dataset which contains grades of students across different subjects:

library(dplyr)

# Example data frame with class and grades
data <- data.frame(
  Subject = c('Math', 'Math', 'Math', 'History', 'History', 'History'),
  grade = c(85, 90, 78, 88, 92, 85)
)

# Calculate standard deviation for each class
grouped_sd <- data %>%
  group_by(Subject) %>%
  summarise(Standard_Deviation = sd(grade))

print(grouped_sd)

Output:

# A tibble: 2 × 2
  Subject   Standard_Deviation
  <chr>              <dbl>
1 History           3.511885
2 Math              6.027714       

Finding Column-Wise Standard Deviation 

In R, there are a number of different ways to find column-wise standard deviation. To find the SD of specific columns, you can use apply the sd() function. A more efficient way is to use the summarise() or summarise_all() functions of the dplyr package.

Example using apply():

data_frame <- data.frame(A = c(1, 2, 3), B = c(4, 5, 6))
apply(data_frame, 2, sd)

Example using dplyr:

library(dplyr)
data_frame %>%
  summarise(across(everything(), sd))

Weighted Standard Deviation

Now imagine that you are a manager of a sports league where a team has 5 players while others have 50 players. If you calculate the SD of scores across the entire league and treat all teams equally, the 5-player teams would contribute just as much to the calculation as the 50-player teams, even when they have far fewer players.

Such an analysis will be misleading, therefore we need a measure like weighted standard deviation which controls for the weights based on the size of the teams, ensuring that teams with more players contribute proportionally to the overall variability.

The formula for calculating the weighted standard deviation is as follows:

Dw=∑wi​(xi​−μw​)2∑wi​​​

Where:

  • 𝑤i ​represents the weight for each data point,
  • 𝑥i ​ denotes each data point,
  • μw is the weighted mean, calculated as:
μw​=​∑wi​xi∑wi​​

Though R does not have a built-in function for measuring weighted standard deviation, it can be computed manually.

Manually Find Weighted Standard Deviation

Let's say we have test grades data with corresponding weights, and we want to measure the weighted standard deviation:

# Example data with grades and weights
grades <- c(85, 90, 78, 88, 92, 85)
weights <- c(0.2, 0.3, 0.1, 0.15, 0.1, 0.15)

# Calculate the weighted mean
weighted_mean <- sum(grades * weights) / sum(weights)

# Calculate the squared differences from the weighted mean
squared_differences <- (grades - weighted_mean)^2

# Calculate the weighted variance
weighted_variance <- sum(weights * squared_differences) / sum(weights)

# Calculate the weighted standard deviation
weighted_sd <- sqrt(weighted_variance)

print(weighted_sd)

Output:

[1] 3.853245

Conclusion

Standard deviation is quite easy to calculate, despite those cruel sums and roots in the formula, and even easier to interpret. If you just want to make friends with statistics or data science, then like it or not, you also have to make friends with standard deviation and how to measure it in R. 

R
30.01.2025
Reading time: 7 min

Similar

R

The which() Function in R Programming

The which() function is a built-in function in R that returns the indices of the TRUE values in a logical vector. It is particularly useful when you need to know the exact positions of elements that satisfy certain conditions within vectors, matrices, or arrays. The which() function is versatile and can be combined with other functions to perform complex operations on data structures, making it a staple in the R programming toolbox. Syntax of which() The syntax of the which() function is straightforward: which(x) Here, x is a logical vector, which is typically the result of a logical condition applied to a data structure. The which() function then returns the indices of the elements in x that are TRUE. Parameters: x: A logical vector or condition that results in a logical vector. Return Value: A vector of integer indices corresponding to the TRUE elements in x. Basic Usage Examples To understand the basic usage of the which() function, let's consider a simple example with a numeric vector: # Create a numeric vectornumbers <- c(2, 4, 6, 8, 10)# Find the indices of elements greater than 5indices <- which(numbers > 5)# Print the resultprint(indices) In this example, which(numbers > 5) will return the indices of elements in the numbers vector that are greater than 5. The output will be: [1] 3 4 5 This output indicates that the 3rd, 4th, and 5th elements of the vector satisfy the condition numbers > 5. Using which() with Logical Vectors The which() function is commonly used with logical vectors directly. Logical vectors are vectors that contain TRUE or FALSE values. Here's an example: # Create a logical vectorlogical_vec <- c(TRUE, FALSE, TRUE, FALSE, TRUE)# Use which() to find TRUE indicestrue_indices <- which(logical_vec)# Print the resultprint(true_indices) The output will be: [1] 1 3 5 This indicates that the TRUE values are located at the 1st, 3rd, and 5th positions in the vector. Combining which() with Other Functions The which() function becomes even more powerful when combined with other functions. For example, you can use it with the min() or max() functions to find the position of the minimum or maximum value in a vector: # Create a numeric vectorvalues <- c(15, 8, 22, 6, 18)# Find the index of the minimum valuemin_index <- which(values == min(values))# Print the resultprint(min_index) The output will be: [1] 4 This indicates that the minimum value (6) is located at the 4th position in the values vector. You can also use which() in combination with conditional statements to filter data, as shown in this example: # Create a data framedf <- data.frame(Name = c("Alice", "Bob", "Charlie", "David"),                Age = c(25, 32, 18, 29))# Find the index of rows where Age is greater than 20age_indices <- which(df$Age > 20)# Print the resultprint(age_indices) This will return: [1] 1 2 4 Here, the which() function helps identify the rows in the data frame where the Age column is greater than 20. Practical Applications The which() function has various practical applications in R programming: Data Subsetting: You can use which() to subset data frames or vectors based on conditions. Finding Missing Values: Identify the positions of NA or missing values in a data set for cleaning purposes. Conditional Operations: Perform operations on specific elements in a data structure that meet a certain condition. Loop Control: Use which() within loops to control the flow based on conditions. For example, to identify and remove rows with missing values in a data frame: # Create a data frame with NA valuesdf <- data.frame(Name = c("Alice", "Bob", NA, "David"),                Age = c(25, 32, 18, NA))# Find rows with missing valuesna_indices <- which(is.na(df$Name) | is.na(df$Age))# Remove rows with missing valuesdf_clean <- df[-na_indices, ]# Print the cleaned data frameprint(df_clean) The result is a cleaned data frame without the missing values. Performance Considerations While the which() function is efficient for small to moderately sized data, performance can be an issue with very large datasets, particularly when used in loops or repeatedly on large vectors. In such cases, consider optimizing your code by: Vectorization: Use vectorized operations instead of loops where possible. Pre-allocation: Avoid growing objects within loops, which can slow down performance. Parallel Processing: For very large datasets, consider parallel processing techniques to improve performance. Common Pitfalls and How to Avoid Them While which() is straightforward, there are common pitfalls that programmers should be aware of: Empty Results: When no elements satisfy the condition, which() returns an integer(0). Be sure to handle this case in your code to avoid errors. empty_indices <- which(c(FALSE, FALSE, FALSE))print(empty_indices) # Returns integer(0) Handling NA Values: The presence of NA values can lead to unexpected results. Use the na.rm = TRUE argument in functions like min() or max() to handle NA values, or filter them out beforehand. values <- c(15, 8, NA, 6, 18)min_index <- which(values == min(values, na.rm = TRUE)) Logical Length Mismatch: Ensure that the logical vector passed to which() matches the length of the data structure being indexed to avoid mismatched results. Conclusion The which() function in R is a versatile and powerful tool for identifying the positions of elements that meet specific conditions. Whether you're subsetting data, filtering results, or performing conditional operations, understanding how to effectively use which() can greatly enhance your ability to manipulate and analyze data in R. By being aware of common pitfalls and considering performance implications, you can leverage which() to write efficient and error-free R code.
28 August 2024 · 5 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support