一件事
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语句:当calmonth =“ 01”时,获取id既使用产品“ apple”又使用产品“ htc”的交集
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语句是什么?
如果您更喜欢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'
")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句