How I can obtained by dividing each original value by the square root of the sum of squared original values for that column in the original matrix.
data(longley)
X <- as.matrix(longley[,-7])
X/sqrt(colSums(X^2))
Getting wrong results.
Try this:
t(t(X)/sqrt(colSums(X^2)))
Benchmarks:
library(microbenchmark)
microbenchmark(t(t(X)/sqrt(colSums(X^2))),
apply(X, 2 , function(x) x/sqrt(sum(x^2))))
# Unit: microseconds
# expr min lq median uq max neval
# t(t(X)/sqrt(colSums(X^2))) 28.783 33.1305 34.9455 40.5640 68.147 100
# apply(X, 2, function(x) x/sqrt(sum(x^2))) 100.307 105.1940 106.9975 108.1075 193.015 100
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With