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?
apply
apply(matrix1, 1, "%*%", t(matrix1))
@RuiBarradas I tried
apply(matrix1, 1, "%*%", t(matrix1)) I got an error. What is this supposed to do?– Amazonian
Jun 30 at 16:54
apply(matrix1, 1, "%*%", t(matrix1))
It multiplies each row of
matrix1 with its transpose. It was meant as an example of making apply work here. You need to tell us the output you are expecting.– Rui Barradas
Jun 30 at 16:58
matrix1
apply
@RuiBarradas got it! I was trying to get the row-wise sum and row-wise product of the matrix and also understand what the documentation meant, but this answers it!
– Amazonian
Jun 30 at 17:24
1 Answer
1
matrix1 = matrix(rnorm(3*4), 3, 4)
apply(matrix1, 1, "+")
Does something like the transpose because it supplies rows of the matrix1 object one by one and returns the values of each operation as columns. If you had on the other hand specified:
apply(matrix1, 2, "+")
There would not have been the appearance of transposition because apply always returns its values as a column-major result.
In the second instance, you didn't give a second argument to the "%*%" operator. The "+" operator can be either unary or binary but the "%*%" operator is always binary. (It doesn't really make a lot of sense to use "%*%" with apply and a single dimension since "%*%" is really designed as a standalone operator. If you want the row-wise sum then just use:
%*%
%*%
%*%
apply
%*%
rowSums(matrix1)
But you could have used the slower:
apply(matrix1, 1, sum)
For product use:
apply(matrix1, 1, prod)
Neither + nor %*% are designed to collapse their arguments into a single value in contrast to sum and prod which are so designed.
%*%
sum
Reply to comment. The %*% operator performs the "matrix multiply" operation. The i-columns of the first argument are multiplied by the J-rows and summed to deliver the i-j element of a new matrix. Many mathematical operations with statistical or physical meaning which would otherwise require a double for-loop can be accomplished by matrix-multiply. Let's imagine your matrix was a bunch of data values and you wanted to come up with a prediction for each row based on a model with three coefficients equal to say, c(5,6,7):
%*%
c(5,6,7) %*% matrix1
# [,1] [,2] [,3] [,4]
#[1,] 2.047344 10.02339 1.73618 0.7223964
can you explain what %*% is supposed to do? Or give an example of how to use it?
– Amazonian
Jun 30 at 16:57
@Amazonian
%*% performs matrix multiplication. See the help for and examples of the function via ?`%*%`– MHammer
Jul 1 at 6:52
%*%
?`%*%`
@MHammer: was there something wrong with my edit?
– 42-
2 days ago
@42 When I came across the post I didn't anything explaining what
%*% did, so wanted to clarify that to the OP. If your edit was there when I made my comment, then I must have missed it, and my apologies.– MHammer
2 days ago
%*%
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Note that the first
applyreturns a vector and in R that means a column vector. As for the multiply, you need two operands, like inapply(matrix1, 1, "%*%", t(matrix1)).– Rui Barradas
Jun 30 at 16:50