SQLDF如何追加新的向量?

时间:2014-02-14 19:11:01

标签: sql r dataframe sqldf

看到我有一个像这样的data.frame:

str(veh)
$VEHAGE  int  3 6 35 35 5 3 7 4 5 1 ...
$VEHTYPE int  2 1 3 1 2 3 1 4 2 3 ...

然后我跑

a<-sqldf("SELECT VEHAGE, COUNT(VEHAGE) as count1
                             FROM veh 
                             WHERE VEHTYPE==1
                             GROUP BY VEHAGE")


a<-sqldf("SELECT VEHAGE, COUNT(VEHAGE) as count234
                             FROM veh 
                             WHERE VEHTYPE==2|3|4
                             GROUP BY VEHAGE")

我得到了这样的结果

head(a)
  VEHAGE count234
1     -9     92
2      1     79
3      2    164
4      3    212
5      4    260
6      5    199

在这种情况下我失去了count1。但我想要一个像这样的头(a)

  VEHAGE count1   count234
1     -9     92    510
2      1     79    844
3      2    164    123
4      3    212    123
5      4    260    100
6      5    199    100

任何想法?提前谢谢!

2 个答案:

答案 0 :(得分:2)

我想你正在寻找这个:

# first query
a <- sqldf("SELECT VEHAGE, COUNT(VEHAGE) as count1
                             FROM veh 
                             WHERE VEHTYPE==1
                             GROUP BY VEHAGE")

# second query
b <- sqldf("SELECT VEHAGE, COUNT(VEHAGE) as count234
                             FROM veh 
                             WHERE VEHTYPE IN (2, 3, 4)
                             GROUP BY VEHAGE")

# combine
a <- sqldf("SELECT * 
            FROM a JOIN b
            USING (VEHAGE)")

答案 1 :(得分:0)

尝试类似:

a<-sqldf(
 "SELECT VEHAGE, 
    SUM(if(VEHTYPE==1, 1, 0)) as count1,
    SUM(if(VEHTYPE==2|3|4, 1, 0)) as count234
  FROM veh 
  GROUP BY VEHAGE")

虽然免责声明,我不熟悉sqldf语法,但这样的东西在MySQL中可行(尽管在MySQL ==2|3|4中必须是in(2, 3, 4))。

此外,您可能希望添加HAVING count1 | count234之类的内容,以摆脱那些类别中没有车辆的VEHAGE