rrandomdata-generation

r generate random poisson values


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.


Solution

  • 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.