I'm new to R and I find it ambiguous whether I should use a vector as the argument to call a function or use sapply instead to give the variables in the vector to the function one by one.
Isn't it the same thing? Why sapply exists if it's the same thing? Is there times when I should use one way or the other and how to know which way to use?
This question came to my mind because I was writing this
sapply(1:3, function(i) dnorm(i,0,1))
Then I accidentally discovered that I could do
dnorm(1:3,0,1)
How could I know it if I didn't discover it by accident (in order not to do the same mistake with other functions )?
Discovering that I tried to change in the same way this code
kappa <- c(1,2,3,4,5,6,7)
sapply(kappa, function(t)
optimize(function(x) (t*x^22+5*x+6), c(-10,10))$minimum)
to this
kappa <- c(1,2,3,4,5,6,7)
optimize(function(x) (kappa*x^22+5*x+6), c(-10,10))$minimum
but it didn't work!
Please I need a good explanation.
Thank you