I have df1 and df2 and 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

The results should be that fnm in 1 and 3 should be 0. How to correct it?
Thanks

有帮助吗?

解决方案

you checked a.fn1 != ' ' which results to T for 1 and 3 because the value is '' not ' '. try a.fn1 != ''

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top