Let's say I have a matrix, mat.
mat <- matrix(1:5, nrow = 10, ncol = 3, byrow = TRUE)
And I have some sort of function that I want to apply, in this case by column.
getRMSE <- function(true, est) {
sqrt(mean((true - est)^2))
}
(This function is just the most recent example, but I've had this exact same conundrum at least 5 more times.)
If you want to apply a function over a matrix, you use apply. But what if you want to apply a function over a matrix with different values for 'x' in the apply?
In this case, the goal would be that apply would perform the equivalent of this:
getRMSE(mat[,1], 1)
getRMSE(mat[,2], 2)
getRMSE(mat[,3], 3)
But I always run into problems when giving a vector as a supplementary argument to the function. Obviously
apply(mat, 2, getRMSE, c(1,2,3))
isn't going to work, because it will recycle the numbers within the columns, too. But
apply(mat, 2, getRMSE, rep(c(1,2,3), 25))
also doesn't work, which I thought at least had a shot.