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

About Hongwei Xu

I'm a social demographer, a single-child, a husband, and a father.
This entry was posted in Research. Bookmark the permalink.

发表评论

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