I need lapply to pass (to a function) values stored in a vector, successively.
values <- c(10,11,13,10)
lapply(foo,function(x) peakabif(x,npeaks=values))
You want to use mapply
for this: mapply(peakabif, x=foo, npeaks=values)
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
.)
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...)