survey_B <- data.frame("q1" = c(5, 3, 2, 7, 1, 9),
"q2" = c(4, 2, 2, 5, 1, 10),
"q3" = c(2, 1, 4, 2, 9, 10),
"q4" = c(10, 5, 2, 10, 4, 2),
sd_B2 <- t(apply(survey_B, 2, function(x) x/sd(x)))
mean_B2 <- t(apply(survey_B, 2, function(x) x/mean(x)))
for(i in 1:nrow(survey_B)) {
for(j in 1:ncol(survey_B)) {
survey_B[i,j] = survey_B[i,j]-mean_B2[i,j] / sd_B2[i,j]
}
}
I would like to standardize my dataframe by using for loops. I get the mean and sd for columns, but I cannot divide the mean with the sd. I always receive an error message "out of boundaries".
Can someone advise me, please?