I am trying to understand how the weighing works (probability argument) in the sample() function in R.
# seed
set.seed(123)
# some numbers
x = c(1:100)
# some probabilties
p = seq(from = 0, to = 1, length.out = 100)
# sample size
n = 10000
# sample 10000 times from x with probability p
x1 <- sample(x, size = n, replace = TRUE, prob = p)
# plot
hist(x1)
plot(density(x1))
Now is this approximately what the probability argument does?
# adding weights manually
weight = rep(x, n * p)
# now sample from the weighted vector
x2 <- sample(weight, n, replace = TRUE)
# plot
hist(x2)
plot(density(x2))
The plots look very similar so it seems that might be the case.

EDIT
I think I made a mistake when I added the weights manually, i.e. I used n*p but I think I should have used x*p. Then the results do change a bit:
# adding weights manually
weight = rep(x, x * p)
# now sample from the weighted vector
x2 <- sample(weight, n, replace = TRUE)


