Posts

Showing posts with the label sum

R: how to use symbols as a FUN for apply?

R: how to use symbols as a FUN for apply? In R's documentation for apply, it says: FUN: the function to be applied: see ‘Details’. In the case of functions like +, %*%, etc., the function name must be backquoted or quoted. I don't understand the latter half the sentence. When I do matrix1 = matrix(rnorm(3*4), 3, 4) apply(matrix1, 1, "+") I get the transpose of the matrix And when I do apply(matrix, 1, "%*%") I get an error. I'm trying to get the row-wise sum and product of this matrix. Also, if that's not what the documentation is talking about, what do does + and %*% supposed to do when supplied as the FUN argument of apply? Note that the first apply returns a vector and in R that means a column vector. As for the multiply, you need two operands, like in apply(matrix1, 1, "%*%", t(matrix1)) . – Rui Barradas Jun 30 at 16:50 ...