我的目标是使用'matr',按列c1对其进行排序,并保持唯一(c1),其中c2 = 1.例如,从此代码...
c1 = c("a",'a','a','b','b','b','b','c','c')
c2 = c(1,1,0,1,1,0,1,0,0)
matr = as.data.frame(cbind(c1,c2))
one = sqldf('select distinct(c1),c2 from matr where c2 = 1')
vs = sqldf('select distinct(c1),c0,c2 from matr group by c1')
sqldf('select matr.*,one.* from matr
left outer join one
where one.c1 = matr.c1')
到达:
c1 c2
a 1
b 1
c 0
由于某种原因,我在左连接中丢失了行c。我只能到达(通过其他联接)
c1 c2
a 1
b 1
答案 0 :(得分:5)
您希望在最后一个sql语句中使用on
而不是where
。使用where
执行连接,然后应用where
子句,而使用on
则执行相对于on
条件的连接。
> sqldf('select matr.*, one.* from matr left outer join one on one.c1 = matr.c1')
c1 c2 c1 c2
1 a 1 a 1
2 a 1 a 1
3 a 0 a 1
4 b 1 b 1
5 b 1 b 1
6 b 0 b 1
7 b 1 b 1
8 c 0 <NA> <NA>
9 c 0 <NA> <NA>