Created
February 16, 2023 21:28
-
-
Save necronet/32e6e35a3bbfa5bf17b3cd6ac83b52b5 to your computer and use it in GitHub Desktop.
Randomly simulate a matrix representing books per kids on school
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate 10 schools | |
schools <- 1:10 | |
# Sample 100 books of 20 types | |
books <- sample(1:20, size = 100, replace=T) | |
books_per_school <- length(books) / length(schools) | |
# Kid in school | |
kids_in_school <- rbinom(100, size = length(schools), prob = 0.5) | |
#hist(kids_in_school) | |
## Let's say that each school has 10 books requirement so now we need to assign | |
## each kid a set of book based on their school index | |
books_for_kids_in_school <- list() | |
interval_book <- split(books, rep(1:books_per_school, each = books_per_school)) | |
for(i in 1:length(kids_in_school)) { | |
print(paste0("Kid ", i, " Is in school ", kids_in_school[i])) | |
school <- kids_in_school[i] | |
books_for_kids <- interval_book[school] | |
print(paste0("Books grabed are: ", list(books_for_kids))) | |
books_for_kids_in_school = append(books_for_kids_in_school, list(books_for_kids)) | |
} | |
final_matrix_association <- matrix(unlist(books_for_kids_in_school), length(kids_in_school),books_per_school) | |
write.table(final_matrix_association, "matrix.txt", sep = "\t", quote = FALSE, row.names = FALSE, col.names = FALSE) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment