什么是响应SQL'in'语句的R语句?

时间:2014-01-07 07:47:21

标签: sql r

案例

df=data.frame(id=c(101,102,102,103,104,104,104),
         calmonth=c('01','01','01','01','01','01','02'),
         product=c('apple','apple','htc','htc','apple','htc','nokia'),
         bb=sample(1:20,7))

> df
   id calmonth product bb
1 101       01   apple  4
2 102       01   apple  9
3 102       01     htc  8
4 103       01     htc  5
5 104       01   apple 16
6 104       01     htc 19
7 104       02   nokia 20

sql语句:获取id同时使用产品" apple"和产品" htc"当calmonth =" 01"

select id from df where calmonth='01' and product="apple"  and id in 
(select id from df where product="htc" and calmonth="01")

预测结果

   id calmonth     product
1 102       01 apple & htc
2 104       01 apple & htc

那么响应的R语句是什么?

2 个答案:

答案 0 :(得分:3)

如果您更喜欢SQL语法,请使用sqldf包:

library(sqldf)
sqldf("
      select * 
      from (
            select id,
                   calmonth,
                   group_concat(product, ' & ') product
            from df
            group by id, calmonth
           ) 
      where product='apple & htc' and
            calmonth='01'
      ")

答案 1 :(得分:2)

您的SQL语句不会产生您显示的结果。它只返回(选择)ID而不是该表和合并列?否?

SELECT id 
FROM   df 
WHERE  calmonth = '01' 
       AND product = "apple" 
       AND id IN (SELECT id 
                  FROM   df 
                  WHERE  product = "htc" 
                         AND calmonth = "01") 
R中的

大致是:

with(df, 
intersect(
id[calmonth=='01' & product=='apple'], 
id[product=="htc" & calmonth=="01"]))

[1] 102 104