【问题标题】:sum if the time difference is smaller than the value by id and by type如果时间差小于 id 和 type 的值,则求和
【发布时间】:2017-10-16 10:48:42
【问题描述】:

关于这个问题: Sum if the date difference is smaller than a value 感谢@Davis Vaughan,现在我可以计算前 12 小时内发生的事件数量:

df <- tribble(
  ~fechayhora,        ~id,       ~tipo,
  "2017-03-17 08:03:00", "A",  "APF",
  "2017-05-17 10:34:00", "A",  "APF",
  "2017-05-17 12:52:00", "A",  "APF",
  "2017-05-17 08:52:00", "A",  "APP",
  "2017-05-17 10:52:00", "A",  "APP",
  "2017-05-17 10:46:00", "B",  "APP",
  "2017-05-17 14:23:00", "B",  "APP",
  "2017-05-17 17:29:00", "B",  "APF"
)

df <- df %>%
  mutate(fechayhora = as.POSIXct(fechayhora),
         minus_12   = fechayhora - hours(12))

df <- df %>% mutate(
  number_of_APF_12h = map2_dbl(.x = fechayhora, 
                               .y = minus_12, 
                               .f = ~sum(between(df$fechayhora, .y, .x)) - 
1)) 

然后我尝试做同样的事情,但按“id”和“tipo”(类型)分组。我试过数据表和数据框,没有成功:

df=df[,number_of_failures_12h = map2_dbl(.x = fechayhora, 
                               .y = minus_12, 
                               .f = ~sum(between(df$fechayhora, .y, .x)) - 
1)),by=.(tipo,id)]

df <- df %>%
group_by(id,tipo)
%>% mutate(
  number_of_failure = map2_dbl(.x = fechayhora, 
                               .y = minus_12, 
                               .f = ~sum(between(df$fechayhora, .y, .x)) - 
1)) %>%
ungroup()

预期结果:

   fechayhora             id    tipo      n_APP   n_APF
   "2017-03-17 08:03:00", "A",  "APF",    0       0 
   "2017-05-17 10:34:00", "A",  "APF",    0       1
   "2017-05-17 12:52:00", "A",  "APF",    0       2
   "2017-05-17 08:52:00", "A",  "APP",    0       2
   "2017-05-17 10:52:00", "A",  "APP",    1       2  
   "2017-05-17 10:46:00", "B",  "APP",    0       0
   "2017-05-17 14:23:00", "B",  "APP",    1       0
   "2017-05-17 17:29:00", "B",  "APF"     0       0

谢谢!!

【问题讨论】:

  • 对不起,有很多猜测
  • 如果你想告诉我有什么不清楚的地方谢谢
  • 你是如何使用 dplyr 获得预期输出的,因为我无法获得
  • 这是我想要做的,因为它不起作用:df=df[,number_of_failures_12h = map2_dbl(.x = fechayhora, .y = minus_12, .f = ~sum(between (df$fechayhora, .y, .x)) - 1)),by=.(tipo,id)]
  • ahhh ...我手工编写的预期输出,我无法使用数据表获得它,也无法使用 dplyr :(

标签: r dataframe time datatable purrr


【解决方案1】:

必须有更优雅的方式,但应该这样做:

# Auxiliary function
count_failures <- function(group, last_12, rowid, type) {
   group[1:rowid-1, ] %>% 
    filter(tipo %in% type & fechayhora >= last_12) %>% 
    nrow()
}

split_by_group <- df %>% 
  group_by(id) %>% 
  do(data = (.)) %>% 
  select(data) %>% 
  map(identity) %>% 
  .[[1]]

df_s <- split_by_group %>% 
  map(arrange, fechayhora) %>% 
  map(.f = function(x) { 
    x %>% 
      rowid_to_column() %>% 
      rowwise() %>% 
      mutate(n_APP = count_failures(x, minus_12, rowid, "APP"),
             n_APF = count_failures(x, minus_12, rowid, "APF")) %>% 
      ungroup() %>% 
      select(-rowid)
      }) %>% 
  bind_rows()

输出:

# A tibble: 8 x 6
           fechayhora    id  tipo            minus_12 n_APP n_APF
               <dttm> <chr> <chr>              <dttm> <int> <int>
1 2017-03-17 08:03:00     A   APF 2017-03-16 20:03:00     0     0
2 2017-05-17 08:52:00     A   APP 2017-05-16 20:52:00     0     0
3 2017-05-17 10:34:00     A   APF 2017-05-16 22:34:00     1     0
4 2017-05-17 10:52:00     A   APP 2017-05-16 22:52:00     1     1
5 2017-05-17 12:52:00     A   APF 2017-05-17 00:52:00     2     1
6 2017-05-17 10:46:00     B   APP 2017-05-16 22:46:00     0     0
7 2017-05-17 14:23:00     B   APP 2017-05-17 02:23:00     1     0
8 2017-05-17 17:29:00     B   APF 2017-05-17 05:29:00     2     0

【讨论】:

  • 你好@quartin!非常感谢您的回答,差不多是这样,但结果有两列:n_failures 类型 APP 的数量和 n 失败类型 APF 的数量;也就是说,对于每一行,我想知道发生了多少以前类型的事件......
  • 我已经编辑了答案,你能检查一下输出是否是你想要的吗?您的预期输出和您对所需内容的解释似乎不匹配...
  • @Martu 我在group_by 中有错字。编辑答案时没有正确复制。现在已经更正了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-22
相关资源
最近更新 更多