Skip to content

when to use which apply function?

4 messages · Rui Barradas, David Winsemius, C W

#
Hello,

The correct syntax would be


sapply(1:10, function(i) newfun(x=mat[i, 1], y=mat[i, 2], a=a, b=b))


Hope this helps,

Rui Barradas

Em 26-03-2013 21:51, C W escreveu:
#
On Mar 26, 2013, at 2:51 PM, C W wrote:

            
There is nothing that would naturally "catch" the values. After they are evaluated sequentially by `sapply` the values 1:10 no longer are named "i" and so don't naturally fall into the slots you thought you had constructed for them. By constructing an anonymous function with any formal argument name (as already demonstrated by another poster)  you could get to use positional matching.
Yes, "some other function" since all those operations are vectorized, this could be more easily done with:

 mat[ ,1]*mat[ ,2]+a+b   # and no need for a loop, right?