Reputation: 49
Why
kappa <- c(0.0001,0.001,0.01,0.1,0.5,0.9,0.99)
sapply ( kappa, function (t) t)
and
sapply ( kappa, function (t) print(t))
return different results?
thank you!
Upvotes: 1
Views: 50
Reputation:
You missed a comma in your example but you can try this:
kappa <- c(0.0001,0.001,0.01,0.1,0.5,0.9,0.99)
sapply (X=kappa, FUN = function (t)t)
What is happening in sapply is "Simplify apply" which will simplify ON IT'S OWN BY BASE R - it will try and find the best format to simplify the outcome which in this case is the numbers of kappa as a result of the function
as the outcome is based on the function - function(t) t - this just returns the value of t - which is just kappa
however when you are running a print statement so function(t) print(t) - at each "step" of the apply - it is trying to "simplify" the print call - which is why you get this output one after the other :
sapply ( kappa, function (t) t)
also as user above commented:
They are different because in the second case print(t) does two separate things: it prints t on the console and returns t as the result. The first only returns the result, no printing. So the second case is the same, you're just seeing each item printed along the way before sapply finishes
Upvotes: 1