Reputation: 79
I have a dataset where individuals belong to no group, one group, or more than one group. It looks like this
resp_id G1 G2 G3 G4 G5 G6
1 0 1 1 0 0 1
2 1 1 0 1 0 0
3 0 0 0 0 0 0
4 0 1 0 0 0 0
5 0 1 0 0 0 0
and so on for about 400 rows. Here, '1' signifies groups membership and '0' means they aren't a part of the group. I want to assign individuals to groups such that:
Is there a way I can do this in R?
Upvotes: 0
Views: 69
Reputation: 27802
you mean something like this?
#persons
id = 1:400
# draw 6 groups of 46 unique persons
L <- lapply(split(id, cut(id, 6)), sample, 47, replace = FALSE)
names(L) <- paste0("G", 1:6)
L <- lapply(L, as.data.table)
# bind to a data.table
DT <- rbindlist(L, id = "group", use.names = TRUE, fill = TRUE)
# final approach
final <- data.table(id = id)
final[DT, group := i.group, on = .(id = V1)]
answer <- dcast(final, id ~ group, value.var = "id", fun.aggregate = length)
colSums(answer)
# id NA G1 G2 G3 G4 G5 G6
# 80200 118 47 47 47 47 47 47
head(answer, 20)
# id NA G1 G2 G3 G4 G5 G6
# 1: 1 0 1 0 0 0 0 0
# 2: 2 0 1 0 0 0 0 0
# 3: 3 0 1 0 0 0 0 0
# 4: 4 0 1 0 0 0 0 0
# 5: 5 0 1 0 0 0 0 0
# 6: 6 0 1 0 0 0 0 0
# 7: 7 0 1 0 0 0 0 0
# 8: 8 0 1 0 0 0 0 0
# 9: 9 0 1 0 0 0 0 0
#10: 10 0 1 0 0 0 0 0
#11: 11 0 1 0 0 0 0 0
#12: 12 0 1 0 0 0 0 0
#13: 13 1 0 0 0 0 0 0
#14: 14 0 1 0 0 0 0 0
#15: 15 0 1 0 0 0 0 0
#16: 16 1 0 0 0 0 0 0
#17: 17 0 1 0 0 0 0 0
#18: 18 0 1 0 0 0 0 0
#19: 19 1 0 0 0 0 0 0
#20: 20 0 1 0 0 0 0 0
Upvotes: 1