Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

dataframe - What function can I use to generate student groups with at least one friend in R?

I need your help.

Let's say that I have a list of students that I need to split to 3 groups. I let every student choose up to 3 friends, and guaranteed that every one will have at least one friend of his choice in his group. Obviously, I would like to maximize the amount of total amount of friends connections as much as possible.

E.g:

I create a dataset of 4 columns: the student's name, and 3 friends that he has chosen.

data <- data.frame(matrix(ncol = 4, nrow = 50))
colnames(data) <- c("student.name", "friend1", "friend2", "friend3")

data$student.name <- c("John","Ben","Dan","James","Emma","Dana",
              "Liam","Mohammad","Ethan","Noah","Lucas",
              "Mason","Harper", "William","Billy", "Moses",
              "Eli","Bob","Tilson","Mishel","Joel","Patrick",
              "Ron","Gideon","Yair","Mike","Olivia", "Jacob",
              "Owen","Levi","Sebastian","Logan","Aiden","Matt",
              "Dave","Kenn","Myra","Natali","Lily","Cathrin", "Kevin",
              "Igor","Heron","Kraus","Dosh","Milosh","Jerry","George",
              "Kosmo","Elaine")
data$friend1 <- sample(data$student.name)
data$friend2 <- sample(data$student.name)
data$friend3 <- sample(data$student.name) 

> data
   student.name   friend1   friend2   friend3
1          John    Mishel     Ethan      Igor
2           Ben       Ron       Ben       Eli
3           Dan      Kenn    Olivia     Kraus
4         James      Levi     Mason       Dan
5          Emma     Kosmo      Liam     Kevin
6          Dana     Mason    Natali      Matt
7          Liam     Jacob     Aiden      Kenn
8      Mohammad       Dan   William      Owen
9         Ethan      Mike    Harper   Cathrin
10         Noah      John     Kosmo       Ben
11        Lucas     Jerry Sebastian      Mike
12        Mason     Kraus      Dave    Olivia
13       Harper      Igor    Mishel     Kosmo
14      William     Moses     Kraus      Levi
15        Billy    Harper      Noah     Logan
16        Moses     Ethan      Dana     Moses
17          Eli    Gideon      Myra     Aiden
18          Bob      Liam  Mohammad    George
19       Tilson Sebastian    Gideon   William
20       Mishel    Elaine      John       Bob
21         Joel     Logan     Logan      Myra
22      Patrick       Ben       Bob     Jerry
23          Ron   Cathrin      Emma      Yair
24       Gideon     Lucas      Dosh     Billy
25         Yair      Dana      Yair     Mason
26         Mike    Natali      Matt      Dave
27       Olivia      Matt      Mike      Dosh
28        Jacob    Milosh     Jerry     Lucas
29         Owen     Heron       Eli    Gideon
30         Levi      Yair     Lucas     Jacob
31    Sebastian      Emma      Kenn      John
32        Logan     James     Heron      Emma
33        Aiden     Billy   Cathrin    Elaine
34         Matt     Kevin    Tilson Sebastian
35         Dave     Aiden      Owen      Lily
36         Kenn    Tilson     Billy    Mishel
37         Myra       Eli     Jacob      Liam
38       Natali    George     Moses       Ron
39         Lily   William      Joel    Tilson
40      Cathrin      Dave      Levi    Natali
41        Kevin      Dosh       Dan      Joel
42         Igor      Lily      Igor    Harper
43        Heron      Owen    Milosh      Dana
44        Kraus    Olivia     James     Ethan
45         Dosh      Noah   Patrick      Noah
46       Milosh       Bob       Ron  Mohammad
47        Jerry      Joel    Elaine    Milosh
48       George   Patrick     Kevin     James
49        Kosmo  Mohammad      Lily     Heron
50       Elaine      Myra    George   Patrick
question from:https://stackoverflow.com/questions/65907705/what-function-can-i-use-to-generate-student-groups-with-at-least-one-friend-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...