There are a few potential problems here.
First, are you sure you want to manually set a seed within a loop? The random number generator is supposed to generate random numbers after it started from a seed, if you keep changing the seed I don't think you still have good guarantees that the numbers are random. In general it's a better idea to set the seed once at the beginning, and then let the random number generator do its thing.
Second problem: i++
does not exist in R, you would use i <- i+1
. And even in C you would need to overwrite i in some way, e.g. i = i++
(if you just have i++
, a value of i+1
is created and discarded).
But that's not a problem here: in R the for
loop already increments i
automatically.
Third problem:
mod_M[sample(nrow(mod_M), size=nrow(mod_M)*0.7, replace=F),]
again, you are computing a value, but not saving it. This line has no effect on your script. Maybe you wanted to write something like this?
mod_M_subset <- mod_M[sample(nrow(mod_M), size=nrow(mod_M)*0.7, replace=F),]
M_test_s_Sd[i]=filter (mod_M_subset , !subject.id %in% M_train_s_Sd[i]$subject.id)
not sure, I don't really understand what the code is attempting to do.
Speaking of, if your goal is to split a dataset, taking a dataframe and creating a list of subsets, you might be interested in the split()
function. If your goal is to create random subsamples, maybe you're better off with something like:
n_groups <- 10
groups <- sample(1:n_groups, size = nrow(mod_M), replace = TRUE)
# check the groups
table(groups)
# create list of subsamples
list_of_subsamples <- split(mod_M, f = groups)
or other variation on this.