ddply根据字符串

时间:2017-11-15 16:31:51

标签: r count plyr

我的数据框如下所示:

   userid                           Worktype  dwelltime
1 ABENAVI  Check Hazmat label Msg for carton  0.7666667
2 ARMITAJ  Check Hazmat label Msg for carton  0.3333333
3 ARMITAJ  Check Hazmat label Msg for carton  0.4166667
4 ARMITAJ         scanned to wrong sort rule  0.6500000
5 ARMITAJ         scanned to wrong sort rule  0.4666667
6 ARMITAJ  Check Hazmat label Msg for carton  0.4666667
7 ARMITAJ  Check Hazmat label Msg for carton 12.2333333
8 ARMITAJ  Check Hazmat label Msg for carton  4.5000000

我想知道每个userid "scanned wrong sort rule"worktype的次数。

我的结果看起来像

userid    Worktype  Count
ABENAVI   Scanned to wrong sort rule       2
Nithin    Scanned to wrong sortrule        0 

我写了这段代码,它给了我一个错误

  

" ifelse错误(x [," rf_log2 $ Worktype"] =="扫描到错误的排序规则" ,: unused argument(0)"

我的代码是:

quality <- ddply(rf_log2, .(userid), function(x) c(
              rules <- sum(ifelse(x[ ,"Worktype"] == "scanned to wrong sort rule", x[,"Worktype"],1,0), na.rm = TRUE)))

1 个答案:

答案 0 :(得分:0)

您可以创建一个向量,检查df $ Worktype是否等于"scanned to wrong sort rule",然后使用table获取每userid的计数:

df$check <- (df$Worktype == "scanned to wrong sort rule")
table(df[, c(1,4)])[, 2]
# ABENAVI ARMITAJ 
#       0       2