Apply row-to-row formula within a table in R

Asked

Viewed 52 times

3

I have a table where I need to apply a row-to-row formula to multiple columns, and the formula is:

row value - mean(all column values) / standard deviation(all column values)

is a normalization of the data to be applied in the machine Learning model.

fx <- c(6.9, 6.8, 6.7, 6.0, 6.8)

The calculation would be in this case:

6,9 - media(fx) / standard deviation(fx)... 6,8 - media(fx) / standard deviation(fx)... 6,7 - media(fx) / standard deviation(fx)...

Would anyone have any alternative to this case?

inserir a descrição da imagem aqui

1 answer

4


The R has a specific function for this called scale:

fx <- c(6.9, 6.8, 6.7, 6.0, 6.8)
fx_scale <- scale(fx)
fx_scale
#>            [,1]
#> [1,]  0.7129310
#> [2,]  0.4387268
#> [3,]  0.1645225
#> [4,] -1.7549072
#> [5,]  0.4387268
#> attr(,"scaled:center")
#> [1] 6.64
#> attr(,"scaled:scale")
#> [1] 0.3646917

as.vector(fx_scale)
#> [1]  0.7129310  0.4387268  0.1645225 -1.7549072  0.4387268

Created on 2021-01-29 by the reprex package (v0.3.0)

  • 1

    Thanks @Marcus Nunes, helped me a lot, I tried to apply in several columns of my dataset, but it didn’t work, I tried to use the function round(apply(df[6:],2,Scale)), because I want to pick up the column from the 6 until the end of the dataset, know some way I could do this?

  • 1

    Use scale(df[, 6:ncol(df)])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.