根据R中的单独逻辑变量对连续周数进行计数

pdkcd3nj  于 2023-06-19  发布在  其他
关注(0)|答案(2)|浏览(148)

在data.framedf中,我需要构造列计数。对于同一customer_id,它应该递增地计数两个Buy=TRUE之间的连续Buy=FALSE。然而,如果对于相同的customer_id没有先前的Buy=TRUE,则它应该是NA。一个data.table或dplyr解决方案会很好。

df <- data.frame(
  customer_id = rep(1:2, each = 6),
  Buy = c(FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE),
  count = c(NA, NA, 0, 1, 2, 0, NA, 0, 1, 0, 0, 1)
)
ryevplcw

ryevplcw1#

library(dplyr)
df |>
  mutate(
    consec_id = consecutive_id(Buy),
    any_true = cumsum(Buy) > 0,
    .by = customer_id
  ) |>
  mutate(
    result = case_when(
      Buy ~ 0L,
      any_true ~ row_number(),
      TRUE ~ NA_integer_
    ),
    .by = c(customer_id, consec_id)
  )
#    customer_id   Buy count consec_id any_true result
# 1            1 FALSE    NA         1    FALSE     NA
# 2            1 FALSE    NA         1    FALSE     NA
# 3            1  TRUE     0         2     TRUE      0
# 4            1 FALSE     1         3     TRUE      1
# 5            1 FALSE     2         3     TRUE      2
# 6            1  TRUE     0         4     TRUE      0
# 7            2 FALSE    NA         1    FALSE     NA
# 8            2  TRUE     0         2     TRUE      0
# 9            2 FALSE     1         3     TRUE      1
# 10           2  TRUE     0         4     TRUE      0
# 11           2  TRUE     0         4     TRUE      0
# 12           2 FALSE     1         5     TRUE      1
r3i60tvu

r3i60tvu2#

一种方法可能是

df$count <- NULL
library(dplyr)

df %>% 
  group_by(grp = cumsum(Buy), customer_id) %>% 
  mutate(count = row_number() - 1, grp = Buy[1] == F) %>% 
  ungroup() %>% 
  mutate(count = ifelse(grp, NA, count), grp = NULL)
# A tibble: 12 × 3
   customer_id Buy   count
         <int> <lgl> <dbl>
 1           1 FALSE    NA
 2           1 FALSE    NA
 3           1 TRUE      0
 4           1 FALSE     1
 5           1 FALSE     2
 6           1 TRUE      0
 7           2 FALSE    NA
 8           2 TRUE      0
 9           2 FALSE     1
10           2 TRUE      0
11           2 TRUE      0
12           2 FALSE     1

相关问题