【问题标题】:ddply summarize count based on stringddply 根据字符串汇总计数
【发布时间】:2017-11-15 16:31:50
【问题描述】:

我的数据框如下所示:

   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"] == "扫描到错误的排序规则", : 未使用的参数 (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)))

【问题讨论】:

    标签: r count plyr


    【解决方案1】:

    您可以创建一个向量来检查 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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多