lundi 22 mars 2021

Generate random groups with condition on other group column

I have the following data with 10 IDs all being assigned to a group ranging between 1 and 5.

library(data.table)

ID <- c(1:10)
Group_1 <- c(1, 1, 1, 2, 3, 3, 4, 4, 5, 5)

df <- as.data.table(cbind(ID, Group_1))
view(df)

ID Group_1
1  1
2  1
3  2
4  2
5  3
6  3
7  4
8  4
9  5
10 5

Now I wish to randomly assign an additional group to each ID, again with a number ranging between 1 and 5. However, with the condition that Group_2 is not the same as Group_1.

ID Group_1 Group_2
1  1  3
2  1  5
3  2  4
4  2  1
5  3  2
6  3  2
7  4  5
8  4  1
9  5  3
10 5  4



Aucun commentaire:

Enregistrer un commentaire