Are there are functions in R that arrange observations in groups of N that reflect, as closely as possible, the data set proportions of certain variables?
For example, if I have a data set with 8 observations and two variables each with two levels with data set proportions as follows:
Var1 Var2
1 0.5 0.5
2 0.5 0.5
Are there any functions that would enable me to optimally sample from the data set to say create groups of 2 observations that reflect the above data set proportions?
Example data:
Data <- read.table(text=" Obs Var1 Var2
1 1 1
2 1 2
3 2 1
4 2 2
5 1 1
6 1 2
7 2 1
8 2 2 ", header=T)
Desired Result:
Result <- read.table(text=" Obs Var1 Var2 Group_ID
1 1 1 1
4 2 2 1
2 1 2 2
3 2 1 2
5 1 1 3
7 2 1 3
6 1 2 4
8 2 2 4 ", header=T)
Not that all groups have proportions of .5 for each level of each variable.