Reputation: 12673
I want to sample from a big pool of numbers e.g. -2000:5000
.
I would like thought to set the weight for a certain number to 80%
.
Everything other possibility should be equal for every other number.
Doing this for a small sample is easy:
sample(-2:2, 10, replace = TRUE, prob=c(0.05, 0.05, 0.80, 0.05, 0.05))
this would output:
[1] 0 0 0 0 0 -1 0 0 0 0
How can I do this for a big range of numbers?
Upvotes: 1
Views: 57
Reputation: 368
You only need to run a separate vector with all the probabilities. Take on the account that when you create the probabilities vector, you can use a logic test to define "p1 = 0.8" if it is my desired number and "p2 = 0.2/n" in any other case. The code run as follows:
n <- 37 # your num
x <- -100:1000 # your sequence
probs <- ifelse(x == n, 0.8, (1 - 0.8) /length(x) ) # determine probabilities taking on account they have to add up to 1
sum(probs)
sample(x = x, size = 1000, prob = probs)
Let me know if it helps.
Upvotes: 2
Reputation: 206167
Just manipulate your probability vector pragmatically. So you have
values <- -2:2
special_value <- 0
Then you can do
probs <- rep(1, length(values))
probs[values==special_value] <- (length(values )-1)*.8/(1-.8)
Then use
sample(values, 10, replace = TRUE, prob=probs)
Upvotes: 2