I have a data frame that looks like this. For each ID, I want to randomly assign subjects into two groups with relatively equal subjects, and I also want to add a new column that indicates which group they're in. For example, For ID 1, 101 and 103 are assigned into Group A, 102 and 104 are in Group B; for ID 2, 105 and 106 are in Group A, 107 is in Group B. And I have thousands of IDs and subjects, how can I manage to do this?
ID subject
1 1 101
2 1 102
3 1 103
4 1 104
4 2 105
5 2 106
6 2 107