0

While applying a function on different datasets, I have realized for dataset with consequent amount of observation, a couple of observations are not included in the output (See y previous question st_network_blend does not include all points). So my idea is to filter the original dataset and restart the operation, bind the second output to the first one and repeat the process as long as observations are not included.

I have tried the function on below. Where I apply the function distfn() that I have created, on the original dataframe. After, the difference between the output and the original, is extracted as Diff. If the length of the vector Diffis 0 then the output does not need correction. If the length is higher than 0, then the loop has to start to filter, apply the code, bind outputs and assess the difference with the original dataframe.

Distloop <- function(dfInj){
  dist  = distfn(dfInj)
  Diff = setdiff(Inj$ID, dist$ID)
  x <- as.numeric(length(Diff))
  if(as.numeric(length(Diff)) == 0){
    dist
  } else {
    while(x != 0){
      dfInjb <- dfInj %>% filter(ID == Diff)
      distb <- distfn(dfInjb)
      dist <- dist %>% rbind(distb)
      Diff = setdiff(Inj$ID, dist$ID)
      x <- as.numeric(length(Diff))
    }
  }
}

The problem come from the while function...

P.S.: Unfortunately, due to the size of the dataset needed to illustrate the problematic, I cannot join any reproducible example. Nevertheless, you can take a look at the example given in that question st_network_blend does not include all points

C. Guff
  • 418
  • 3
  • 18
  • 4
    There appears to be no return value after the `while` loop. Does changing the last three lines to `}; dist; }; }` solve your problem? Incidentally, a recursive solution might be a neater solution here... It would be helpful if you specified exactly what "The problem" is. – Limey Apr 18 '23 at 16:50
  • 1
    Thank you, it works! If you want more detail, I have updated the linked question where I included the corrected code as an answer. – C. Guff Apr 18 '23 at 23:29

0 Answers0