生成R中所有可能的行组合?

时间:2017-08-04 03:18:34

标签: r

我们说我有两个数据框,学生和老师。

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"))

我希望生成所有可能的学生和教师组合,并保留随附的信息,基本上创建数据框和#34;学生&#34;的所有行组合。和#34;老师&#34;。这可以通过循环和cbind轻松完成,但对于大型数据帧,这需要永远。帮助R新手 - 最快的方法是什么?

编辑:如果这不清楚,我希望输出具有以下格式:

rbind(
  cbind(students[1, ], teachers[1, ]), 
  cbind(students[1, ], teachers[2, ]) 
  ...
  cbind(students[n, ], teachers[n, ]))

3 个答案:

答案 0 :(得分:2)

  

并保留随附的信息

我建议不要这样做。没有必要在一个对象中包含所有内容。

只是将老师和学生结合起来,那就是

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

要合并其他数据(我建议在必要时不要这样做):

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")]

这给出了

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

答案 1 :(得分:2)

您可以将所有数据组合如下:

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 :(得分:0)

您可以使用此功能

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

expand.grid.df(students,teachers)