What's the difference between lapply and do.call?

前端 未结 7 1139
孤独总比滥情好
孤独总比滥情好 2020-11-29 14:46

I\'m learning R recently and confused by two function: lapplyand do.call. It seems that they\'re just similar to map function in Lisp.

相关标签:
7条回答
  • 2020-11-29 15:18

    lapply is similar to map, do.call is not. lapply applies a function to all elements of a list, do.call calls a function where all the function arguments are in a list. So for a n element list, lapply has n function calls, and do.call has just one function call. So do.call is quite different from lapply. Hope this clarifies your issue.

    A code example:

    do.call(sum, list(c(1, 2, 4, 1, 2), na.rm = TRUE))
    

    and:

    lapply(c(1, 2, 4, 1, 2), function(x) x + 1)
    
    0 讨论(0)
提交回复
热议问题