0
set.seed(4286)
n <- 4
k <- 5
V <- sample(seq(4), size=k, replace=TRUE)
M <- matrix(rnorm(n*k), ncol=k)
X <- M
for(i in seq(n)){
    X[i,] <- round(M[i,]/V, 2)
}

How can I use apply family to do the same job above instead of using for loop?

d.b
  • 32,245
  • 6
  • 36
  • 77
T.T
  • 27
  • 1
  • 4

1 Answers1

0

This is the apply equivalent. The results need to be transposed by default.

t(apply(M, 1, function(x) round(x/V, 2)))