使用dplyr处理来自不同数据帧的相同变量

时间:2017-06-18 14:33:41

标签: r dataframe dplyr left-join missing-data

我有两个共享相同键的数据帧,我可以使用left_join或其他连接函数将它们合并以合并数据帧。但是我在两个数据框中都有相同的变量,并且它们的缺失方式不同。这是我的例子:

df1 <- data.frame(key1= c("A", "B" , "C", "D", "E","F", "G" , "H", "I", "J"), 
                  key2 = c("a", "b", "c", "d", "e","f", "g", "h", "i", "j"), 
                  foo = c(NA, NA, 21 , 25,21,22, NA, 23 , 25, NA))
df2 <- data.frame(key1= c("F", "G" , "A", "B", "J"), 
                      key2 = c("f", "g", "a", "b", "j"), 
                      foo = c(NA, NA, 21 , 25,21))

当我致电left_join(df1, df2)时,它会给我以下数据框

   key1 key2 foo
1     A    a  NA
2     B    b  NA
3     C    c  21
4     D    d  25
5     E    e  21
6     F    f  22
7     G    g  NA
8     H    h  23
9     I    i  25
10    J    j  NA

正如你所看到的,我有&#34; A a&#34;丢失的密钥对,但是df2用&#34; A a&#34;密钥对,它不会丢失。我怎样才能以正确的方式加入?

0 个答案:

没有答案