有效过滤最近/最近一周的最佳方法是什么(基于可能不是整整一周的数据)。

library(lubridate)
library(dplyr)

df <- data.frame(dates =
c("2014-12-17","2014-12-18","2014-12-21","2014-12-25","2014-12-26",
  "2015-05-17","2015-05-18","2015-05-21","2015-05-25","2015-05-26",
  "2016-06-17","2016-06-18","2016-06-21","2016-06-25","2016-06-26"))


df <- df %>% mutate(dates = ymd(dates),
                    the.year = year(dates),
                    the.week = week(dates))

#Filter the last week (as may not be complete)

我可以提出这样的解决方案
max.week <- df %>% filter(the.year == max(the.year)) %>%
  filter(the.week == max(the.week)) %>%
  group_by(the.year, the.week) %>%
  summarise(count= n()) %>%
  ungroup() %>%
  mutate(max.week = paste(the.year, the.week,sep="-")) %>%
  select(max.week) %>%
  unlist(use.names = F)

df %>% filter(!paste(the.year, the.week, sep = "-") == max.week)
   %>%

但是必须有一个更简单的解决方案?

最佳答案

group_indices也可以帮助:

df %>%
  filter(group_indices(., the.year, the.week) < max(group_indices(., the.year, the.week)))

也可以写成:
df %>% filter({id <- group_indices(., the.year, the.week)} < max(id))

要么
df %>%
  mutate(id = group_indices(., the.year, the.week)) %>%
  filter(id < max(id))

关于r - dplyr编程方式过滤最近一周,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45168342/

10-11 08:15