Reputation: 2358
I just ran the following code and am confused by the result:
> N = 6000
> my.vect = numeric(N)
> sum(my.vect[1:6000])
> for ( i in 1:100)
+ {
+ screen = sample(6000, 50, replace=FALSE)
+ my.vect[screen] =+ 1
+ }
> sum(my.vect[1:6000])
[1] 3415
I expected that after the for loop and finished, there should be 50 * 100 new additions to my.vect; instead there are only 3415 additions. The only explanation I can come up with is that setting replace=FALSE means that values chosen twice NEVER get replaced. If this is the case, how can I modify my code to do replacement between samplings of 50?
Upvotes: 0
Views: 34
Reputation: 145775
R doesn't have a =+
increment operator.
x =+ 1
is equivalent to
x = (+1)
x = 1
So in your code, you set my.vect[screen]
equal to 1, and you do it 100 times.
Each time, you set 50 random indices to 1. You're explanation with replace = F
is opposite of what makes sense... each time you select 50 unique random indices, but in each iteration some of the indices you select may overlap with previous selections, so you are adding (probably) fewer than 50 new 1's each time.
This can be seen easily if you do a smaller example and keep track of each iteration. I've done that below where each iteration is a row of a matrix:
N = 20
k = 10
my.mat = matrix(0, nrow = k, ncol = N)
my.mat
set.seed(47)
for (row in 1:k) {
screen = sample(N, 4, replace=FALSE)
my.mat[row, screen] = 1
}
my.mat
colSums(my.mat) # you can see that many indices were chosen multiple times
# but if you used my random seed, one column was never chosen
# your final result from above is equivalent to
colSums(my.mat) > 0
I'm not clear what you want to do.
If your goal is to increment, you could do it in a matrix as I did and use the colSums
, or you could
set
my.vect[screen] = my.vect[screen] + 1
If you want to choose different values each time, do all the sampling up front, before the loop
screen = sample(6000, 50 * 100, replace = FALSE)
and then inside your loop use
my.vect[screen[ seq(50 * (i - 1) + 1, 50 * i) ]] = 1
(though the loop doesn't do much at that point).
Upvotes: 1