Let's say I have 3 classes, a,b, and c in that I want to be distributed in a 3:2:1 ratio, respectively. A vector containing a minimally 'balanced' set of these classes in this ratio would look something like this:
class_1<-"a"
class_2<-"b"
class_3<-"c"
ratio_a<-3
ratio_b<-2
ratio_c<-1
min_set<-c(rep(class_1,ratio_a),rep(class_2,ratio_b),rep(class_3,ratio_c))
This minimum set would look something like this:
min_set
"a""a""a""b""b""c"
If I were to make a df containing all permutations of min_set
as such:
library(combinat)
library(data.table)
myList <- permn(min_set)
all_out <- data.table(matrix(unlist(myList),byrow = T,ncol = 6))
and then create a matrix containing my three classes, called block_1
:
lamb<-10
block_1<-matrix(0,lamb,length(min_set))
for (i in 1:lamb){
block_1[i,]<-min_set
}
and then randomly sample from block_1
, storing the results in a vector, block_2
:
block_2<-vector('numeric',length=dim(block_1)[1]*dim(block_1)[2])
How could I create a while loop that stops once I have the min_set
in block_2
?
I've tried something like this, but I can't get it to work:
for (i in 1:(dim(block_1)[1]*dim(block_1)[2])){
while (min_set %in% block_2=F){
#while block_2 does not contain the min_set
block_2[i]<-sample(as.vector(block_1),i,replace=F)
}
}
I was thinking of using all_out (all permutations of min_set
) as part of a solution, as well.
Aucun commentaire:
Enregistrer un commentaire