I have a matrix
df <- matrix(c(9185, 3661, 9539, 4266, 9650, 3897, 9160, 4451), nrow = 2, ncol = 4)
and I want calculate: (element - mean(by row))/ sd(by row) . Ex: (9185 - 9383,5)/248,03 and substitute the result in a matrix. Like that:
matrix(c(-0.80030564, -1.144503446, 0.62694, 0.553656, 1.074466, -0.482080851, -0.9011, 1.072928123), nrow = 2, ncol = 4)
The problem is that in the real case I need to iterate and I don't know how many columns I will have, but they will be all the columns present in the df dataframe.
You can use apply()
to do row-wise calculations on a matrix.
df <- matrix(c(9185, 3661, 9539, 4266, 9650, 3897, 9160, 4451), nrow = 2, ncol = 4)
t(apply(df, 1, function(x)(x-mean(x))/sd(x)))
#> [,1] [,2] [,3] [,4]
#> [1,] -0.8003056 0.6269397 1.0744658 -0.9010998
#> [2,] -1.1445034 0.5536562 -0.4820809 1.0729281
Also, scale()
is a function that does the calculation you want
t(apply(df, 1, scale))
#> [,1] [,2] [,3] [,4]
#> [1,] -0.8003056 0.6269397 1.0744658 -0.9010998
#> [2,] -1.1445034 0.5536562 -0.4820809 1.0729281
Created on 2023-10-02 with reprex v2.0.2