Hello, I had expected that the following should work mat <- matrix(c(5, 7, 6, 7, 8, 10, 17, 28, 36, 41, 46, 140), nrow = 2, byrow = T) mat apply(mat, 2, function(e1,e2) e1*e2) apply(mat, 2, function(e1,e2) e1+e2) apply(mat, 2, function(e1,e2) e1-e2) apply(mat, 2, function(e1,e2) e1/e2) but I get Error in FUN(newX[, i], ...) : Argument "e2" is missing, with no default What do I do wrong? Sincerely Fredrik Lundgren
Fredrik Lundgren wrote:> apply(mat, 2, function(e1,e2) e1*e2)> but I get > > Error in FUN(newX[, i], ...) : Argument "e2" is missing, with no default > > What do I do wrong?Misunderstand what 'apply' does perhaps? It applies your function to columns (the '2') of the matrix, and so the function should only expect one argument, for example: > apply(mat, 2, function(e1){print(e1)}) [1] 5 17 [1] 7 28 [1] 6 36 [1] 7 41 [1] 8 46 [1] 10 140 - in this toy example,my function is called with e1 set to columns of the matrix. So what were you trying to do with apply(mat, 2, function(e1,e2) e1*e2)? Baz