Can lapply pass (to a function) values stored in a vector, successively

落爺英雄遲暮 提交于 2019-12-01 19:37:33

You want to use mapply for this: mapply(peakabif, x=foo, npeaks=values)

There are a couple of ways to handle this. You could try a straight indexing vector approach.

lapply(1:length(foo), function(i) peakabif(foo[i], npeaks=values[i]))

(and someone already beat me to the mapply version...)

Sometimes you can convert an existing function so that it will accept vectors (or lists) using Vectorize

vrep <- Vectorize(rep.int)
> vrep(list(1:4, 1:5), list(2,3) )
[[1]]
[1] 1 2 3 4 1 2 3 4

[[2]]
 [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

(Under the hood it's really a convenience wrapper for mapply in the same way the read.table is a wrapper for scan.)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!