奇怪的group_by + mutate + which.max行为 [英] Weird group_by + mutate + which.max behavior

查看:43
本文介绍了奇怪的group_by + mutate + which.max行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遇到 dplyr 的意外行为:

I'm running into unexpected behavior with dplyr:

library(dplyr)

df <- structure(list(date = c("2016-05-02", "2016-05-02", "2016-05-02", 
      "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", 
      "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", 
      "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", 
      "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", 
      "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", 
      "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", 
      "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", 
      "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", 
      "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", "2016-05-02", 
      "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", 
      "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", 
      "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", 
      "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", 
      "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", 
      "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", 
      "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", 
      "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", 
      "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-03", 
      "2016-05-03", "2016-05-03", "2016-05-03", "2016-05-04", "2016-05-04", 
      "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", 
      "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", 
      "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", 
      "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", 
      "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", 
      "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", 
      "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", 
      "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", 
      "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", "2016-05-04", 
      "2016-05-04", "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", 
      "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", 
      "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", 
      "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", 
      "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", 
      "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", 
      "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", 
      "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", 
      "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", 
      "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-05", "2016-05-06", 
      "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", 
      "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", 
      "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", 
      "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", 
      "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", 
      "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", 
      "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", 
      "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", 
      "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", "2016-05-06", 
      "2016-05-06", "2016-05-06"), abc = c(NA, NA, NA, NA, NA, NA, 
         NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
         NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
         NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, 0, 0, 0, 0, 0, 0, 
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 20, 20, 16, 
         14, 9, 8, 6, 5, 5, 6, 7, 13, 24, 52, 65, 68, 66, 65, 58, 47, 
         21, 6, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
         1, 1, 0, 0, 0, 0, 0, 10, 19, 19, 15, 11, 8, 8, 5, 4, 4, 4, 5, 
         9, 17, 31, 43, 49, 52, 52, 47, 32, 21, 6, 2, 1, 1, 1, 1, 1, 1, 
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 5, 14, 
         14, 14, 15, 18, 18, 14, 14, 14, 15, 19, 29, 46, 58, 62, 69, 71, 
         67, 56, 40, 25, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
         2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 10, 18, 18, 14, 12, 9, 7, 5, 
         4, 5, 5, 7, 9, 17, 30, 36, 49, 52, 54, 54, 42, 32, 15, 5, 1)), 
     class = "data.frame", row.names = c(NA, -240L), .Names = c("date", "abc"))


df %>%
  group_by(date) %>%
  mutate(peak_max_index = as.numeric(which.max(as.numeric(abc))))

我希望这返回的是 peak_max_index ,对于 date 2016-05-04 .但是奇怪的是, peak_max_index NA .更奇怪的是,如果在运行 dplyr 命令之前将所有 date 2016-05-03 的行踢出,结果将完全是正确的.这是一个错误吗?

What I would expect this to return is peak_max_index that's 41 for all rows where date is 2016-05-04. But strangely peak_max_index is NA instead. Even more strangely, if you kick out all rows where date is 2016-05-03 before you run the dplyr commands, the result is entirely correct. Is this a bug?

推荐答案

您正在公式 which.max()中评估 NA的.只需用!is.na()消除 NA's .

You are evaluating NA's in your formula which.max(). Just eliminate the NA's with !is.na().

df %>%
    group_by(date) %>%
    mutate(peak_max_index = max(df$abc[!is.na(df$abc)]))

这篇关于奇怪的group_by + mutate + which.max行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆