Reputation: 143
I have a dataset in which I work with mean-centered and standardized versions of many of the variables. In my r code I have a large list of the scale() functions that I run for all of the variables but I am wondering if there is a way to write a simple function that will optimize this process.
For example: instead of having a huge list like this...
df$Z.ROW1 <- scale(df$ROW1, scale=T)
df$Z.ROW2 <- scale(df$ROW2, scale=T)
df$Z.ROW3 <- scale(df$ROW3, scale=T)
.....
Is there a way to write a function that will create new vectors and append them to the end of the data frame based on the variables I specify to be standardized?
I found this example online:
set.seed(212)
df = matrix(rnorm(15), 5, 5))
colnames(df) <- c("ROW1", "ROW2", "ROW3", "ROW4", "ROW5")
df
ROW1 ROW2 ROW3 ROW4 ROW5
[1,] -0.2391731 0.1544909 0.1503488 -0.2391731 0.1544909
[2,] 0.6769356 1.0368712 0.5096765 0.6769356 1.0368712
[3,] -2.4403360 -0.7796077 -0.7733148 -2.4403360 -0.7796077
[4,] 1.2408845 0.6212641 1.8756660 1.2408845 0.6212641
[5,] -0.3265144 0.2994313 0.7883057 -0.3265144 0.2994313
center.scale <- function(z) {
scale(z, scale = T)
}
center.scale(df[,c("ROW1", "ROW2")])
ROW1 ROW2
[1,] -0.01534097 -0.1657064
[2,] 0.63734894 1.1398052
[3,] -1.58357932 -1.5477370
[4,] 1.03913941 0.5249004
[5,] -0.07756806 0.0487378
Which gets close but it doesn't solve the issue of creating new vectors and appending them to the end of my existing dataset. Ideally, I would like it so that the only thing I need to change is the variable names in the center.scale() function. Thanks!
Upvotes: 3
Views: 1107
Reputation: 10437
Here is a version that doesn't hard code scale
options, and lets you select a subset of the original columns. It returns a data.frame
since that will be useful in more situations, though you can easily modify it to return a matrix
if you wish.
add_scaled <- function(data, vars = colnames(data), ...) {
data.frame(data,
setNames(data.frame(scale(data[, vars, drop = FALSE],
...)),
paste("Z", vars, sep = ".")))
}
By default it returns a data.frame
with all columns standardized and appended.
df = matrix(rnorm(15), 5, 3)
colnames(df) <- paste0("Col", 1:ncol(df))
df
## Col1 Col2 Col3
## [1,] 1.9659082 -1.2254071 0.1477912
## [2,] 0.2666273 -0.9123931 1.4747579
## [3,] 1.0813351 2.4138457 -1.5569830
## [4,] 0.9618084 1.3076966 -0.8646893
## [5,] -2.0246095 0.3043559 -1.3617747
add_scaled(df)
## Col1 Col2 Col3 Z.Col1 Z.Col2 Z.Col3
## 1 1.9659082 -1.2254071 0.1477912 1.0040228 -1.05411792 0.4625295
## 2 0.2666273 -0.9123931 1.4747579 -0.1216110 -0.84828629 1.5207917
## 3 1.0813351 2.4138457 -1.5569830 0.4180659 1.33898111 -0.8970361
## 4 0.9618084 1.3076966 -0.8646893 0.3388893 0.61159985 -0.3449285
## 5 -2.0246095 0.3043559 -1.3617747 -1.6393669 -0.04817676 -0.7413566
If only some columns should be standardize you may select them.
add_scaled(df, vars = c("Col1", "Col3"))
## Col1 Col2 Col3 Z.Col1 Z.Col3
## 1 1.9659082 -1.2254071 0.1477912 1.0040228 0.4625295
## 2 0.2666273 -0.9123931 1.4747579 -0.1216110 1.5207917
## 3 1.0813351 2.4138457 -1.5569830 0.4180659 -0.8970361
## 4 0.9618084 1.3076966 -0.8646893 0.3388893 -0.3449285
## 5 -2.0246095 0.3043559 -1.3617747 -1.6393669 -0.7413566
Finally, you can pass arguments to scale
, so you don't lose any flexibility.
add_scaled(df, vars = "Col1", center = FALSE, scale = TRUE)
## Col1 Col2 Col3 Z.Col1
## 1 1.9659082 -1.2254071 0.1477912 1.2353890
## 2 0.2666273 -0.9123931 1.4747579 0.1675502
## 3 1.0813351 2.4138457 -1.5569830 0.6795177
## 4 0.9618084 1.3076966 -0.8646893 0.6044064
## 5 -2.0246095 0.3043559 -1.3617747 -1.2722773
add_scaled(df, vars = "Col1", center = TRUE, scale = FALSE)
## Col1 Col2 Col3 Z.Col1
## 1 1.9659082 -1.2254071 0.1477912 1.5156943
## 2 0.2666273 -0.9123931 1.4747579 -0.1835866
## 3 1.0813351 2.4138457 -1.5569830 0.6311212
## 4 0.9618084 1.3076966 -0.8646893 0.5115945
## 5 -2.0246095 0.3043559 -1.3617747 -2.4748234
Upvotes: 3
Reputation: 5201
If I understood your question correctly, you can cbind
the output of scale
to the original data, as @Dason suggests.
Example:
> df <- data.frame(ROW1 = c(1,2,1,1), ROW2 = c(1,2,3,4), ROW3 = c(5,8,6,5))
> df
ROW1 ROW2 ROW3
1 1 1 5
2 2 2 8
3 1 3 6
4 1 4 5
> df <- cbind(df, scale(df, scale = T))
> names(df)[4:6] <- paste0('Z.', names(df)[4:6])
> df
ROW1 ROW2 ROW3 Z.ROW1 Z.ROW2 Z.ROW3
1 1 1 5 -0.5 -1.1618950 -0.7071068
2 2 2 8 1.5 -0.3872983 1.4142136
3 1 3 6 -0.5 0.3872983 0.0000000
4 1 4 5 -0.5 1.1618950 -0.7071068
Upvotes: 1
Reputation: 8072
Like @Dason said, you just need to modify your function to cbind
in your original data, and name the new columns accordingly.
center.scale <- function(z) {
x <- scale(z, scale = T)
colnames(x) <- paste0("scale_", colnames(x))
cbind(z, x)
}
center.scale(df[,c("ROW1", "ROW2")])
Results in:
ROW1 ROW2 scale_ROW1 scale_ROW2
[1,] -0.2391731 0.1544909 -0.01534097 -0.1657064
[2,] 0.6769356 1.0368712 0.63734894 1.1398052
[3,] -2.4403360 -0.7796077 -1.58357932 -1.5477370
[4,] 1.2408845 0.6212641 1.03913941 0.5249004
[5,] -0.3265144 0.2994313 -0.07756806 0.0487378
Upvotes: 2