0

I have created a function which works normally when I take it out from the function. However, when I put that in the function and the output is always wrong:

  ele_remover <- function(original, remove){
  output <- original[-remove]
  output
}

  ICD4 <- function(num.plots = 240, num.crops = 6, remove.crop.combo){
  combination.a <- apply(combn(1:num.crops,2),2,paste0,collapse="")
  combination.b <- apply(combn(num.crops:1,2),2,paste0,collapse="")
  
  new.combination <- c(combination.a,combination.b)
  
  combination <- ele_remover(new.combination, remove.crop.combo)
  
  comb.x <- sort(as.numeric(combination))
  comb.each <- ceiling(num.plots/length(comb.x))
  crop.combinations <- rep(comb.x, each=comb.each)

  output <- comb.x
}

  ICD4_output <- ICD4(240, 6, c(12,13))

Before the elements is removed from the vector "new.combination", it should be like (11, 12, 13 .... 64, 65); after the function working removed the elements, some of the elements should be removed from the new.combination. So that the final output should contain the left elements in the vector.

I have tried all this outside function, it worked finely! But when I put it back in the function, the removing function never worked normally. Hope to get your support!

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 02 '23 at 13:56
  • @MrFlick, thank you! I have edited please have a check! – Aoxiang Xin Feb 02 '23 at 13:57
  • 2
    I edited your function to make the last line `return(list(new.comb = as.numeric(new.combination), final_output = comb.x))` so we can see both the unremoved `new.combination` and the removed `comb.x`. You can then clearly see that 2 elements have been removed. The 12th and 13th elements are `36` and `45` and the are not in the final output. I'm not sure how what you want is different from what your code is doing, but `ele_remover` is certainly working as I expect it to, removing the `remove`-indexed elements from its input. – Gregor Thomas Feb 02 '23 at 14:45
  • Maybe you want to remove by value, not by index? `output <- original[!remove %in% original]`? – Gregor Thomas Feb 02 '23 at 14:47

1 Answers1

-1

Just a quick note since your ICD4 function ends in

...
  output <- comb.x
}

and not

...  
output <- comb.x
output
}

there could be all sorts of weird behavior happening depending on the objects in your workspace?

GWD
  • 1,387
  • 10
  • 22
  • 1
    This is bad practice but not the cause of any problem. (Rather than `output <- comb.x; output` as the last line, may as well just use `comb.x`. Renaming it to `output` does nothing useful. But if the last line of a function is an assignment, the value assigned will be returned.) – Gregor Thomas Feb 03 '23 at 17:39
  • I know it sounds weird but I could reproduce the behavior the OP witnessed 2 days ago and fix it with adding an explicit `output` call, but I no longer remember which R version I ran it on (on my PC at work w R-4.1.3 or R-4.0.5) or how or in what way I 'cluttered' up (or mistreated) my workspace? but otherwise your remarks are correct as well as that the OP's code basically should work correctly in the form shown above, which it does on my home machine with R-4.2.2; "ghosts in the machine"? – GWD Feb 05 '23 at 20:37
  • This isn't something that's changed with R versions. I can't tell you what you ran to think this was the issue, but this has been R's behavior for at least the 15 years I've used it. – Gregor Thomas Feb 06 '23 at 14:22