Create all possible string combinations in R?

Say I have two data frames, students and teachers.

students <- data.frame(name = c("John", "Mary", "Sue", "Mark", "Gordy", "Joey", "Marge", "Sheev", "Lisa"), height = c(111, 93, 99, 107, 100, 123, 104, 80, 95), smart = c("no", "no", "yes", "no", "yes", "yes", "no", "yes", "no")) teachers <- data.frame(name = c("Ben", "Craig", "Mindy"), height = c(130, 101, 105), smart = c("yes", "yes", "yes")) 

I want to create all possible combinations of students and teachers and save the accompanying information, basically to create all combinations of strings from "frames" of data and "teachers". This can easily be done with a loop and cbind, but for a massive data frame it takes forever. Help newbie R - what's the fastest way to do this?

Edit: if this is not clear, I want the output to have the following format:

 rbind( cbind(students[1, ], teachers[1, ]), cbind(students[1, ], teachers[2, ]) ... cbind(students[n, ], teachers[n, ])) 
+5
source share
3 answers

You can combine all the data as shown below:

 do.call(cbind.data.frame,Map(expand.grid,teacher=teachers,students=students)) name.teacher name.students height.teacher height.students smart.teacher smart.students 1 Ben John 130 111 yes no 2 Craig John 101 111 yes no 3 Mindy John 105 111 yes no 4 Ben Mary 130 93 yes no 5 Craig Mary 101 93 yes no 6 Mindy Mary 105 93 yes no : : : : : : : : : : : : : : 
+2
source

and save the accompanying information

I would recommend not to do this. There is no need to have everything in one object.

To simply combine teachers and students, there is

 res = expand.grid(teacher_name = teachers$name, student_name = students$name) 

To combine other data (which I would recommend doing until needed):

 res[, paste("teacher", c("height", "smart"), sep="_")] <- teachers[match(res$teacher_name, teachers$name), c("height","smart")] res[, paste("student", c("height", "smart"), sep="_")] <- students[match(res$student_name, students$name), c("height","smart")] 

This gives

 head(res) teacher_name student_name teacher_height teacher_smart student_height student_smart 1 Ben John 130 yes 111 no 2 Craig John 101 yes 111 no 3 Mindy John 105 yes 111 no 4 Ben Mary 130 yes 93 no 5 Craig Mary 101 yes 93 no 6 Mindy Mary 105 yes 93 no 
+2
source

You can use this function

 expand.grid.df <- function(...) Reduce(function(...) merge(..., by=NULL), list(...)) expand.grid.df(students,teachers) 
0
source

Source: https://habr.com/ru/post/1270560/


All Articles