r generate random poisson values

60 Views Asked by At

How do I randomly generate 50% 1s, 30% 2s, 15% 3s and remaining 4s ?

Finally when I do a table , table(x, useNA = "ifany"), it should be

                         1      2      3    4
                         50     30     15   5

I am not sure how to use rpois to generate this.

1

There are 1 best solutions below

2
jay.sf On BEST ANSWER

Using sample.int.

set.seed(42)
x <- sample.int(n=4, size=1e4, replace=TRUE, prob=c(.5, .3, .15, 1 - sum(c(.5, .3, .15))))

proportions(table(x))
# x
#      1      2      3      4 
# 0.5001 0.3001 0.1506 0.0492 

If you depend on rpois, you probably need to invent something with optimize.