1

Trying to apply a matrix to a function, using mapply without success

I'm trying to solve a set of equations for different parameters. In a more simplistic form of the set of functions, I'm trying to pass a function to a matrix - constants -

     a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

and trying to solve an equation. A simplified version of the function is

3*a + 2*b + 3*c and return the answer for each row in the matrix.

I have changed the original function to a linear and more simple one - that is why I prefer using #mapply and that's also why former explanations have not assisted me.

building the matrix

my_vector <- 1:9
constants <- matrix(my_vector, 3, 3)
colnames(constants) <- c("a", "b", "c")
constants

the target function

fun_abc <- function(a, b, c){
  return(3 * a + 2 * b + 3 * c)
}

applying constants to the function

mapply(fun_abc, 2, constants)

I keep getting Error in (function (a, b, c) : argument "c" is missing, with no default Can anyone spot the problems?

Amidavid
  • 177
  • 7
  • `mapply` is very different from `apply`: it does not have a `margin=` argument (your 2?); the function is first, not the data; it works on one or more lists/vectors, not a matrix. The `m` in `mapply` does not mean "matrix", not sure if that's what you were thinking. – r2evans Sep 16 '19 at 13:39
  • 2
    I see that you reposted the question from https://stackoverflow.com/questions/57944843/applying-a-matrix-to-a-function again. Can you explain why the previous answer didn't work and it would be better if you could include an example which is failing for current answers. If you post the same question with same example you might get same answers. – Ronak Shah Sep 16 '19 at 13:43
  • 1
    Is this question different than https://stackoverflow.com/questions/57944843/applying-a-matrix-to-a-function and https://stackoverflow.com/questions/8045701/r-matrix-by-vector-multiplication ? – G. Grothendieck Sep 16 '19 at 13:47
  • OP stated that they changed the equation but I could not see any change except a statement stating that they changed the question. Unclear to me what the issue is. – NelsonGon Sep 16 '19 at 13:51
  • Amidavid, you've been active on SO well after these comments have been posted, is there any reason to *not* close this question? If your previous question's answers did not fully address your question, then *address it there*. – r2evans Sep 16 '19 at 15:04
  • Thank you all for the comments. What I meant by the comment of changing the function is that the function that I actually need to apply is much more complicated than the one shown here, meaning, I have simplified the function that I want to apply to a much friendlier, shorter, linear version than the original function. The original function has constraints not shown here. – Amidavid Sep 17 '19 at 08:27

0 Answers0