I have a dataset as such:
data.frame(ID = c("A1","A6","A3","A55","BC","J5","Ca", "KQF", "FK", "AAAA","ABBd","XXF"), Group = paste0("Group",c(1,1,1,1,1,2,2,2,2,2,1,2)))
ID Group
1 A1 Group1
2 A6 Group1
3 A3 Group1
4 A55 Group1
5 BC Group1
6 J5 Group2
7 Ca Group2
8 KQF Group2
9 FK Group2
10 AAAA Group2
11 ABBd Group1
12 XXF Group2
How can I create two sub-dataframes from the above data such that there are no repeats and there are exactly the same number of elements from Group1
and Group2
in each sub-dataframe? Both sub-dataframes combined together are always identical to the original dataframe.
ID is always unique.
EXAMPLE RESULT
subDF1
ID Group
1 A1 Group1
4 A55 Group1
11 ABBd Group1
6 J5 Group2
8 KQF Group2
9 FK Group2
subDF2
ID Group
2 A6 Group1
3 A3 Group1
5 BC Group1
7 Ca Group2
10 AAAA Group2
12 XXF Group2
- Equal number of elements in subDF1 and subDF2
- Equal proportion of elements from Group1 and Group2
- Elements in subDF1 should not be in subDF2 and vice-versa