Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- peopleId <- LETTERS[1:9]
- library(data.table)
- DT <- data.table(giver = peopleId, mark_giver = FALSE, taker = peopleId, mark_taker = FALSE)
- out <- matrix(NA_character_, nrow(DT), 2L)
- for (i in 1L:nrow(DT)) {
- out[i, 1L] <- sample(DT[mark_giver == FALSE]$giver, 1L)
- out[i, 2L] <- sample(DT[mark_taker == FALSE & taker != out[i, 1L]]$taker, 1L)
- set(DT, match(out[i, 1L], peopleId), match("mark_giver", names(DT)), TRUE)
- set(DT, match(out[i, 2L], peopleId), match("mark_taker", names(DT)), TRUE)
- }
- out
- # [,1] [,2]
- # [1,] "C" "D"
- # [2,] "H" "G"
- # [3,] "B" "H"
- # [4,] "A" "E"
- # [5,] "I" "C"
- # [6,] "G" "I"
- # [7,] "F" "B"
- # [8,] "E" "F"
- # [9,] "D" "A"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement