0

How to use function "sapply" with different variables in one function? For example, it is simple to use it with one variable, as is in the following example:

x1 <- c(1,2,3)
sapply(x1, function(x) dnorm(x, mean=2, sd=1))

However, how to apply different variables to some function with sapply? Please find the example below:

 x1 <- c(1,2,3)
 mean1 <- c(1,2,3)
 sd1 <- c(1,2,3)
 ### ???
 sapply( c(x1, mean1, sd1), function (x, mean, sd) dnorm(x = x1, mean = mean1, sd=sd1)

Thank you in advance.

user36478
  • 346
  • 6
  • 14
  • 4
    You're looking for `mapply`. – joran May 21 '14 at 16:04
  • Thank you! Is it also work for lapply (with the lists)? – user36478 May 21 '14 at 16:14
  • That is, if mapply is a multivariate version of sapply, what is for lapply? – user36478 May 21 '14 at 16:29
  • 1
    `sapply` _is_ `lapply`, it just does some extra processing on the results. – joran May 21 '14 at 16:30
  • This question is subset of [R Grouping functions: sapply vs. lapply vs. apply. vs. tapply vs. by vs. aggregate](http://stackoverflow.com/questions/3505701/r-grouping-functions-sapply-vs-lapply-vs-apply-vs-tapply-vs-by-vs-aggrega) – CHP May 21 '14 at 16:44

0 Answers0