dre
dre

Reputation: 504

r: Programatically Normalize & Name Variables

I have a dataset of two variables:

df<- data.frame(var1= rnorm(20, 15, 1.5), var2= rnorm(20, 7.5, 1))

I have a function that I can use to normalize them:

normFunc<- function(x){
  xAvg<- mean(x)
  return(x/xAvg)
}

Running the function manually works fine, see below on var1:

df$var1AVG<- normFunc(df$var1)

enter image description here

What I'd like to do is pass both variables into the function dynamically, using either a for loop, assign them to the dataframe and dynamically name them.

Upvotes: 0

Views: 258

Answers (1)

niko
niko

Reputation: 5281

You can use colMeans/colMeans2 to do this in a vectorized fashion

df / colMeans(df)
# or
df / matrixStats::colMeans2(as.matrix(df))
# yields
     var1      var2
1  0.9307705 0.4228236
2  1.9674104 0.9776180
3  1.1397295 0.4255726
4  2.0279614 0.9090271
5  0.9987836 0.4519303
...

with the data

set.seed(123)
df <- data.frame(var1= rnorm(20, 15, 1.5), var2= rnorm(20, 7.5, 1))

Addendum

To address your question:

Alternatively, the method Vectorize is worth checking out in such a case or the apply family (with vapply being by far the most efficient one) - in your case vapply(1:ncol(df), function (n) normFunc(df[,n]), numeric(nrow(df))) would work. Or a well constructed loop can sometimes be even faster.

Upvotes: 2

Related Questions