#z-score
#create vector of data
data <- c(6, 7, 7, 12, 13, 13, 15, NA, 19, 22)
#find z-score for each data value
z_scores <- (data-mean(data, na.rm = T))/sd(data, na.rm = T)
#display z-scores
z_scores
## [1] -1.21212121 -1.03030303 -1.03030303 -0.12121212 0.06060606 0.06060606
## [7] 0.42424242 NA 1.15151515 1.69696970
data.frame(scale(data))
## scale.data.
## 1 -1.21212121
## 2 -1.03030303
## 3 -1.03030303
## 4 -0.12121212
## 5 0.06060606
## 6 0.06060606
## 7 0.42424242
## 8 NA
## 9 1.15151515
## 10 1.69696970
#create dataframe
df <- data.frame(assists = c(4, 4, 6, 7, 9, 13),
points = c(24, NA, 13, 15, 19, 22),
rebounds = c(5, 5, 7, 8, 14, 15))
#find z-scores of each column
sapply(df, function(df) (df-mean(df, na.rm = T))/sd(df, na.rm = T))
## assists points rebounds
## [1,] -0.92315712 1.17004875 -0.9035079
## [2,] -0.92315712 NA -0.9035079
## [3,] -0.34011052 -1.21338389 -0.4517540
## [4,] -0.04858722 -0.78003250 -0.2258770
## [5,] 0.53445939 0.08667028 1.1293849
## [6,] 1.70055260 0.73669736 1.3552619
scale(df)
## assists points rebounds
## [1,] -0.92315712 1.17004875 -0.9035079
## [2,] -0.92315712 NA -0.9035079
## [3,] -0.34011052 -1.21338389 -0.4517540
## [4,] -0.04858722 -0.78003250 -0.2258770
## [5,] 0.53445939 0.08667028 1.1293849
## [6,] 1.70055260 0.73669736 1.3552619
## attr(,"scaled:center")
## assists points rebounds
## 7.166667 18.600000 9.000000
## attr(,"scaled:scale")
## assists points rebounds
## 3.430258 4.615192 4.427189
#ref https://www.statology.org/z-score-r/