【问题标题】:Mutate case_when in R to create a column of time periods per participant在 R 中改变 case_when 以为每个参与者创建一列时间段
【发布时间】:2022-02-10 22:14:21
【问题描述】:

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

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))

谢谢!

【问题讨论】:

  • 请不要使用数据图像,因为没有大量不必要的努力就无法使用它们。 For multiple reasons 问题应该是可重复的。这使得其他可能想要帮助的人轻松复制数据。查看堆栈溢出指南minimal reproducible exampleHow to Ask。以对象的形式包含一个最小数据集,例如,如果一个数据框为df <- data.frame(…),其中……是您的变量和值,或者使用dput(head(df))Good overview on asking questions

标签: r tidyverse lubridate


【解决方案1】:

使用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)

tidyverse

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多