Example data:
structure(list(ID = c(1, 2, 3, 4, 5), x1 = c(2.6, 3.8, 2.6, 4.3,
2.6), x2 = c(3.2, 3.2, 3.2, 4, 3.2), x3 = c(4.5, 3.5, 4.5, 3,
4.5), x4 = c(2.4, 3, 2.4, 3.1, 2.4), x5 = c(3.8, 2.6, 4.3, 2.6,
4.4), x6 = c(3.2, 3.2, 4, 3.2, 2.2), x7 = c(3.5, 4.5, 3, 4.5,
4), x8 = c(3, 2.4, 3.1, 2.4, 4.3), x9 = c(3.9, 4, 4, 4, 3.9),
x10 = c(4, 3.9, 4, 4, 4)), class = "data.frame", row.names = c(NA,
-5L), variable.labels = structure(character(0), .Names = character(0)), codepage = 65001L)
I'm trying to mean-center only columns 6:10. I would like to ask for your advice on changing the code below that would keep all the 11 columns of the original data, and append the newly centered columns (so in total 16 columns). Currently, the code below only allows for centering the selected columns:
center.scale <- function(c) {
x <- scale(c, center = T, scale = F)
colnames(x) <- paste0(colnames(x), "_c")
cbind(c, x)
}
centered_data <- center.scale(original_data[,c(6:10)])
p.s.
I have tried the code below, but strangely, it keeps on producing centering results that are not accurate.
data2 <- original_data %>%
mutate_at(.vars = colnames(original_data[6:10]),
.funs = list("c" = center_scale))