英文:
r generate random poisson values
问题
如何随机生成50%的1、30%的2、15%的3和剩余的4?
最后,当我创建一个表格时,table(x, useNA = "ifany") 应该如下所示:
    1    2    3    4
   50   30   15   5
我不确定如何使用 `rpois` 来生成这个。
英文:
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
得分: 3
使用 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 
如果你依赖于 rpois,你可能需要用 optimize 创造一些东西。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论