sqldf空白连接会生成意外结果

时间:2014-01-24 19:31:43

标签: r sqldf

我有df1和df2以及sql join:

df1 <- data.frame(fn1 = c('','NA','' ,'BF'),
              ln1 = c('DRFA', 'DEFF','DDFF', 'ANCD'))
 df2 <- data.frame(fn2 = c('','AA','BB', 'BF'),
              ln2 = c('DRFA', 'DEFF','FFFF', 'ANCD'))

library(sqldf)

nm = sqldf("
select a.*, b.*,
  (a.fn1 !=' '  and a.fn1 == b.fn2  ) as fnm,
  (a.ln1 == b.ln2) as lnm
  from df1 a, df2 b
  where (fnm + lnm)>= 1

       ")
 > nm
   fn1  ln1 fn2  ln2  fnm lnm
1       DRFA     DRFA   1   1
2  NA   DEFF  AA DEFF   0   1
3       DDFF     DRFA   1   0
4  BF   ANCD  BF ANCD   1   1

结果应该是1和3中的fnm应为0.如何纠正? 感谢

1 个答案:

答案 0 :(得分:1)

您检查了a.fn1 != ' ',其结果为T的1和3,因为值为''而不是' '。试试a.fn1 != ''