When you are doing rowcosty(p,x[i,],y[i])
, you are indirectly looping on two variables at the same time: x
and y
. apply
, lapply
, sapply
, vapply
all loop on a single variable.
mapply
is the usual approach when looping on two or more variables. You can do:
mapply(rowcosty, split(x, row(x)), y, MoreArgs = list(p = p))
Or you can also loop on a single variable: the index i
.
sapply(1:nrow(x), function(i) rowcosty(p,x[i,],y[i]))