Very simple cycle, generator matrix x [I, j], then x [, I] ^ 2 + x [j],
Why use the apply more slowly than for loop?
Source:
The rm ls (list=())
# generated data sets
X & lt; - cbind (x1=3, x2=c (10000:1, "000))
# encapsulation fun1
Fun1 & lt; - the function (x) {
MyFUN<- the function (x, c1, c2) {
[c1] x ^ 2 + x (c2)
}
F1 & lt; - the apply (x, 1, myFUN, c1='x1', c2='x2')
}
# encapsulation fun2
Fun2 & lt; - the function (x) {
F2 & lt; - (c)
For (I in 1: nrow (x)) {
F2 [I] <- x (I, 1) ^ 2 + x [I, 2)
}
}
# encapsulation fun3
Fun3 & lt; - the function (x) {
F3 & lt; - c (x ^ 2 + x, [1], [2])
}
# statistics of three methods of CPU time consuming,
System. The time (fun1 (x))
System. The time (fun2 (x))
System. The time (fun3 (x))