Nested functions in R – Issues-

Your lapply is a bit off. You need to pass in a function. Right now you are attempting to call mismatch.i(x) and x isn’t defined anywhere. Plus you defined mismatch.i to have additional parameters that you are not passing. It should look like

diff.mat =, lapply(1:nrow(R), function(x) mismatch.i(D, R, x, threshold)))

Here we clearly make a function that lapply can call and pass the value of x to the i= parameter and pass along the result of the values.

Since it is a nested function, you could also leave out the redundant parmaters from the inner function (since they will never change) So you could do

mis.test = function(D, R, threshold) { 
  D =
  R =
  mismatch.i = function(i) {
    dif = purrr::map2_df(D[-1], R[i,-1], `-`)
    dif[dif<0] = 0
    dif = cbind(ID = D[1],R[i,1], dif)
    dif = dif[which(dif$mismatch <= threshold),]
  diff.mat =, lapply(1:nrow(R), function(x) mismatch.i(x)))
  diff.mat =

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top