I am practicing writing my own functions. I tried to create for function for creating z-scores
age <- c(25, 30, 28, 40, 50)
bmi <- c(22, 24, 30, 26, 35)
test.data <- cbind.data.frame(age, bmi)
zscore <- function(x, mean_x, sd_x, zscore_x) {
test.data <- test.data %>%
mutate(mean_x = mean(x, na.rm = T),
sd_x = sd(x, na.rm = T),
zscore_x = (x - mean_x) / sd_x)
}
zscore(x = bmi,
mean_x = mean_bmi,
sd_x = sd_bmi,
zscore_x = zscore_bmi)
I was hope to create a new variable called zscore_bmi that is added to the test.data, but the new variable is not created. Any suggestions?