Home > Net >  How to use apply function from R to normalize data frame?
How to use apply function from R to normalize data frame?

Time:09-28

I have a data frame and I want to do rowise normalization for each row. For example:

row1_new = (row1_old - mean_of_row1)/standard_dev_of_row1.

I wrote the following code for that:

normalize_df <- function(x){
    mean1<- mean(unlist(as.list(x)))
    std1<- sd(unlist(as.list(x)))
    y = (x -  mean1)/std1
    return(y)
}

n_rows <- length(row.names(query_data))
for(i in seq(1, n_rows)){
    query_data[i,]<- query_data[i,]
}

But it seems to be a lot slower and I didn't succeed in using apply function. How can I use apply function to row-wise normalize dataframe?

CodePudding user response:

As an alternative, using the built-in function scale t(scale(t(df))).

CodePudding user response:

Here is another option using rowMeans and rowSds function.

library(matrixStats)
df <- mtcars
(df - rowMeans(df))/rowSds(as.matrix(df))

#                         mpg    cyl disp    hp   drat     wt     qsec     vs     am   gear   carb
#Mazda RX4           -0.16637 -0.447 2.43 1.496 -0.486 -0.510 -0.25117 -0.559 -0.540 -0.484 -0.484
#Mazda RX4 Wag       -0.16784 -0.448 2.43 1.495 -0.487 -0.507 -0.24221 -0.560 -0.542 -0.486 -0.486
#Datsun 710          -0.02053 -0.504 2.17 1.785 -0.508 -0.547 -0.12833 -0.581 -0.581 -0.504 -0.581
#Hornet 4 Drive      -0.21836 -0.412 2.76 0.897 -0.449 -0.447 -0.24304 -0.475 -0.488 -0.450 -0.475
#Hornet Sportabout   -0.30751 -0.402 2.69 1.067 -0.444 -0.442 -0.32228 -0.472 -0.472 -0.446 -0.454
#Valiant             -0.24228 -0.415 2.72 1.000 -0.462 -0.452 -0.21197 -0.487 -0.501 -0.458 -0.487
#...
#...

CodePudding user response:

It is simple to use an anonymous function:

t(apply(mtcars, 1, function(x) (x-mean(x))/sd(x)))
      Mazda RX4 Mazda RX4 Wag  Datsun 710 Hornet 4 Drive Hornet Sportabout    Valiant Duster 360    Merc 240D    Merc 230   Merc 280  Merc 280C
mpg  -0.1663702    -0.1678380 -0.02053465     -0.2183565        -0.3075069 -0.2422777 -0.3696702 -0.005278277 -0.09496285 -0.2208754 -0.2439523
cyl  -0.4465404    -0.4481484 -0.50419828     -0.4122884        -0.4016114 -0.4152404 -0.4209455 -0.464365595 -0.49763495 -0.4511720 -0.4497564
disp  2.4298741     2.4297052  2.17138772      2.7611422         2.6941650  2.7152412  2.4439581  2.746995205  2.43244710  2.3682166  2.3687127
  • Related