Reputation: 4949
Hi I'm trying to use apply on a matrix I call eq; What I like to do is to send a function I made with multiple arguments. Currently when I do this it works:
apply(eq, 1, manydo2)
manydo2 <-function(x){ # do something with the vector x }
however when I try something like this
apply(list("x1"=eq, "r1" = 18), 1, manydo2)
it fails, is there anyway I can pass the row data as well as some other variable say r1 in this case? multiple thanks.
Upvotes: 0
Views: 3209
Reputation: 52637
Assuming you want to pass the row, and a single argument that is the same for each row:
manydo3 <- function(x, r1) NULL
apply(eq, 1, manydo3, r1=18)
If you want different values for the second argument for each row, then you want to split your matrix into rows and pass both the rows and your other argument with mapply
:
mapply(manydo3, split(eq, row(eq)), R)
where length(R) == nrow(eq)
(i.e. R contains r1, r2, etc).
Upvotes: 3