2017-08-04 62 views
5

比方說,我有兩個數據框,學生和老師。在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")) 

我要生成學生和教師的所有可能的組合,並保持附帶信息,基本上是從數據幀創建行的所有組合「學生」和「老師」。這可以很容易地用循環和cbind來完成,但對於大量的數據幀來說,這是永久的。幫助R新手出去 - 做這件事最快的方法是什麼?

編輯:如果這是不明確的,我所要的輸出格式如下:

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

回答

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

,並保持附帶的信息

我建議不這樣做。沒有必要將所有東西都放在一個對象中。

要只是結合教師和學生,還有

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 
+0

邊注意:'logical'類是專爲'yes' /'no'值。見'?邏輯'。 – Frank

+0

有用的功能,但爲了我目前的目的,我想把所有內容保存在一個對象中 –

+1

或者使用'data.table'中的'CJ' – akrun

0

您可以使用此功能

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

expand.grid.df(students,teachers) 
相關問題