calculate the z score by group in r

To calculate the z score for each observation within each group in R, we can use the dplyr package to group the data by a variable and then use the mutate function to calculate the z score for each observation within each group.

Assuming we have a dataframe df with a variable group_var to group by, a variable obs_var to calculate the z score on, and a variable mean_var representing the group mean for each group:

library(dplyr)

df <- df %>%
  group_by(group_var) %>%
  mutate(z_score = (obs_var - mean_var) / sd(obs_var))
110 chars
6 lines

Here, we use the group_by function to group the data by group_var, and then use the mutate function to create a new variable z_score that represents the z score for each observation within each group.

The z score is calculated by subtracting the group mean (mean_var) from each individual observation (obs_var) within the group, and then dividing this difference by the standard deviation (sd(obs_var)) of the observations within the group.

gistlibby LogSnag