为什么 rbinom 在 R 中给我 nas ?

Why does rbinom give me nas here in R?

经过一些调试后,我将问题缩小为以下几点:

  minc <- function(split, population, base, lift = 0.1) {
      control.pop <- population * (1- split)
      test.pop <- population*split

      control.cr <- base
      test.cr <- control.cr*(1+lift)

      #### print statements ####
      print(c(control.pop, control.cr))  # print values of variables
      print( rbinom(1, 5800.0 , 0.1)) # print rbinom for the values
      print( rbinom(1, control.pop, control.cr))  # print rbinom for the variables
    }


    minc(0.8, population = 29000, base = 0.1, lift = 0.1)

给出输出

[1] 5800.0    0.1
[1] 589
[1] NA

也就是说, 我正在打印传递的变量的值,然后是这些值的 rbinom - 它工作正常 - 但是当我对变量执行 rbinom 时,我得到了 NA。这里发生了什么?

Rbinom 想要一个整数,你可以做的大小

control.pop <- as.integer(population * (1 - split)

正如@ben-bolker 指出的那样,使用 round 将四舍五入为整数,而不仅仅是截断:

control.pop <- round(population * (1 - split))