【发布时间】:2014-01-09 09:15:54
【问题描述】:
我的数据如下:
>df2
id calmonth product
1 101 01 apple
2 102 01 apple&nokia&htc
3 103 01 htc
4 104 01 apple&htc
5 104 02 nokia
现在我想计算当calmonth='01' 时products 包含both 'apple' and 'htc' 的ids 的数量。因为我需要的不仅是'apple'和'htc',我还需要'apple'和'nokia'等。
所以我想通过这样的功能来实现这一点:
xandy=function(a,b) data.frame(product=paste(a,b,sep='&'),
csum=length(grep('a.*b',x=df2$product))
)
另外,我制作了一个这样的参数列表:
para=c('apple','htc','nokia')
但问题就在这里。当我传递像
这样的参数时xandy(para[1],para[2])
结果如下:
product csum
1 apple&htc 0
我的预期结果应该是什么
product csum calmonth
1 apple&htc 2 01
2 apple&htc 0 02
那么参数传递的问题在哪里呢?
还有,我怎样才能正确地将calmonth 添加到函数()xandy 中?
仅供参考。这个问题源于我之前的另一个问题
What's the R statement responding to SQL's 'in' statement
评论后编辑
我的预测结果是:
product csum calmonth
1 apple&htc 2 01
2 apple&htc 0 02
【问题讨论】: