使用因子组合在R中连接两个数据帧

时间:2017-05-17 01:05:02

标签: r database lapply

我有接下来的两个数据框:

table_1<-data.frame(col1=round(runif(10, 1, 10)), col2=round(runif(10, 1, 10)),col3=round(runif(10, 1, 10)),col4=round(runif(10, 1, 10)),col5=round(runif(10, 1, 10)),col6=round(runif(10, 1, 10)))
table_2<-data.frame(col1=round(runif(10, 1, 10)), col2=round(runif(10, 1, 10)),col3=round(runif(10, 1, 10)),col4=round(runif(10, 1, 10)),col5=round(runif(10, 1, 10)),col5=round(runif(10, 1, 10)))

comb<-lapply(1:6, function(n) combn(names(table_1),n) )

[[1]]
     [,1]   [,2]   [,3]   [,4]   [,5]   [,6]  
[1,] "col1" "col2" "col3" "col4" "col5" "col6"

[[2]]
     [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]   [,10]  [,11]  [,12]  [,13]  [,14] 
[1,] "col1" "col1" "col1" "col1" "col1" "col2" "col2" "col2" "col2" "col3" "col3" "col3" "col4" "col4"
[2,] "col2" "col3" "col4" "col5" "col6" "col3" "col4" "col5" "col6" "col4" "col5" "col6" "col5" "col6"
     [,15] 
[1,] "col5"
[2,] "col6"

[[3]]
     [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]   [,10]  [,11]  [,12]  [,13]  [,14] 
[1,] "col1" "col1" "col1" "col1" "col1" "col1" "col1" "col1" "col1" "col1" "col2" "col2" "col2" "col2"
[2,] "col2" "col2" "col2" "col2" "col3" "col3" "col3" "col4" "col4" "col5" "col3" "col3" "col3" "col4"
[3,] "col3" "col4" "col5" "col6" "col4" "col5" "col6" "col5" "col6" "col6" "col4" "col5" "col6" "col5"
     [,15]  [,16]  [,17]  [,18]  [,19]  [,20] 
[1,] "col2" "col2" "col3" "col3" "col3" "col4"
[2,] "col4" "col5" "col4" "col4" "col5" "col5"
[3,] "col6" "col6" "col5" "col6" "col6" "col6"

我想要做的是使用“梳子”中发布的所有组合来加入两个表。因此,使用comb [[1]](只有一个组合)的第一场比赛将是:

table_1$col1=table_2$col1 or table_1$col2=table_2$col2 or table_1$col3=table_2$col3 or ....

第二个组合(comb [[2]])将是

table_1$col1=table_2$col1 and table_1$col2=table_2$col2) or (table_1$col1=table_2$col1 or table_1$col3=table_2$col3) .. and so on.

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

lapply(comb, function(y) 
             lapply(1:ncol(y),function(x)  
                   data.frame(table_1[y[,x]], table_2[y[,x]])
             )
       )

这将为您提供一个嵌套的data.frame列表,该列表采用组合来创建数据框,例如

[[2]]
[[2]][[1]]
   col1 col2 col1.1 col2.1
1     6    9      4      7
2     3    7      4      2
3     4    8     10      5
4     5    9      5      5
5     4    9      9      8
6     6    3      3      3
7     9    7      2      6
8     2    5      5      9
9     5    6      2      9
10   10    4      2      7