在 R 中改变 case_when 以创建每个参与者的时间段列

Mutate case_when in R to create a column of time periods per participant

我在三个时间点对参与者进行了测试。我有他们接受测试的日期。我想制作一个列,其中级别为第一、第二和第三。每个参与者都有三个日期,因此每个参与者都不同。数据如下所示:

structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L), time_tested = c("2022-02-05", "2022-02-05", "2022-02-05", 
"2022-02-08", "2022-02-08", "2022-02-08", "2022-02-11", "2022-02-11", 
"2022-02-11", "2022-02-08", "2022-02-08", "2022-02-08", "2022-02-10", 
"2022-02-10", "2022-02-10", "2022-02-13", "2022-02-13", "2022-02-13", 
"2022-02-05", "2022-02-05", "2022-02-05", "2022-02-08", "2022-02-08", 
"2022-02-08", "2022-02-11", "2022-02-11", "2022-02-11")), class = "data.frame", row.names = c(NA, 
-27L))

这就是我想要的结果:

structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L), time_tested = c("2022-02-05", "2022-02-05", "2022-02-05", 
"2022-02-08", "2022-02-08", "2022-02-08", "2022-02-11", "2022-02-11", 
"2022-02-11", "2022-02-08", "2022-02-08", "2022-02-08", "2022-02-10", 
"2022-02-10", "2022-02-10", "2022-02-13", "2022-02-13", "2022-02-13", 
"2022-02-05", "2022-02-05", "2022-02-05", "2022-02-08", "2022-02-08", 
"2022-02-08", "2022-02-11", "2022-02-11", "2022-02-11"), period = c("first", 
"first", "first", "second", "second", "second", "third", "third", 
"third", "first", "first", "first", "second", "second", "second", 
"third", "third", "third", "first", "first", "first", "second", 
"second", "second", "third", "third", "third")), class = "data.frame", row.names = c(NA, 
-27L))

谢谢!

使用 data.table::rleid 获取组 ID,并使用包 english 中的 ordinal 函数将其转换为序号。

基础 R

df$period <- as.numeric(ave(df$time_tested, df$id, FUN = data.table::rleid))
df$english <- english::ordinal(df$period)

整洁宇宙

df %>% 
  group_by(id) %>% 
  mutate(period = data.table::rleid(time_tested), 
         english = english::ordinal(period))

输出

   id time_tested period english
1   1  2022-02-05      1   first
2   1  2022-02-05      1   first
3   1  2022-02-05      1   first
4   1  2022-02-08      2  second
5   1  2022-02-08      2  second
6   1  2022-02-08      2  second
7   1  2022-02-11      3   third
8   1  2022-02-11      3   third
9   1  2022-02-11      3   third
10  2  2022-02-08      1   first
11  2  2022-02-08      1   first
12  2  2022-02-08      1   first
13  2  2022-02-10      2  second
14  2  2022-02-10      2  second
15  2  2022-02-10      2  second
16  2  2022-02-13      3   third
17  2  2022-02-13      3   third
18  2  2022-02-13      3   third
19  3  2022-02-05      1   first
20  3  2022-02-05      1   first
21  3  2022-02-05      1   first
22  3  2022-02-08      2  second
23  3  2022-02-08      2  second
24  3  2022-02-08      2  second
25  3  2022-02-11      3   third
26  3  2022-02-11      3   third
27  3  2022-02-11      3   third