从具有 upper/lower 边界的泊松分布中抽取的样本数

Number from sample to be drawn from a Poisson distribution with upper/lower bounds

R 中工作,我需要创建一个长度为 n 的向量,其中的值是从 lambda=1 的泊松分布中随机抽取的,但下限为 2,上限为6 个(即所有数字将是 2、3、4、5 或 6)。

我不确定该怎么做。我尝试创建一个 for 循环,用范围内的值替换该范围外的任何值:

seed(123)
n<-25 #example length
example<-rpois(n,1)
test<-example #redundant - only duplicating to compare with original *example* values
 for (i in 1:length(n)){
   if (test[i]<2||test[i]>6){
     test[i]<-rpois(1,1)
   }
 }

但这似乎不起作用(在 test 中仍然得到 0 和 1 等)。任何想法将不胜感激!

这是一种使用泊松分布生成 n 数字并将范围外的所有数字替换为范围内的随机数的方法。

n<-25 #example length
example<-rpois(n,1)
inds <- example < 2 | example > 6
example[inds] <- sample(2:6, sum(inds), replace = TRUE)