R使用dplyr选择最小值大于零的行

时间:2017-12-14 19:13:49

标签: r dplyr

尝试按x对行进行分组,并选择最小值大于零的行

 x  Start Time      End Time        Time_Diff
 A  01-01-17 0:14   01-01-17 0:14   3
 A  01-01-17 0:14   01-01-17 21:51  77817
 B  01-01-17 15:59  01-01-17 7:22   -31042
 B  01-01-17 15:59  01-01-17 16:25  1574

尝试:

df1 <- df %>%
    group_by(x) %>%
    slice(which.min(as.numeric(Time_Diff)))

但是,它返回负值。这是我期待的解决方案。任何帮助将不胜感激。

 x  Start Time      End Time        Time_Diff 
 A  01-01-17 0:14   01-01-17 0:14   3
 B  01-01-17 15:59  01-01-17 16:25  1574

1 个答案:

答案 0 :(得分:0)

我认为这也有效

df2 <- df %>%
  group_by(x) %>%
  filter( Time_Diff > 0 ) %>%
  arrange(desc(as.numeric(Time_Diff))) %>%
  slice(n = n())