Make a draw weighted by prob

p = seq(0,.99,.05) # a vector of prob associated with the elements in x
x = seq(99,length.out=length(p),by=-5) # a vector of elements
ix = seq(1,length(x)) # a index vector for x
choosed = sample(x,1,prob=p) # the sampled element in x
x
ix
choosed
ix[x==choosed] # the index of the sampled element in x
Advertisements

About Hongwei Xu

I'm a social demographer, a single-child, a husband, and a father.
此条目发表在Research分类目录。将固定链接加入收藏夹。

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s