![](/img/trans.png)
[英]R dplyr mutate multiple columns using custom function to create new column
[英]Custom mutate new column based on two other columns in R using dplyr
我的目标是创建一个新的 df 列,其值基于其他两列。 我的数据集涉及一项研究的招募。 我想要一个专栏来定义一个人是否在研究的特定轮次中,如果是,则是他们的第一次参与、第二次、第三次等(最多 8 轮)。 目前我正在mutate(case_when))
尝试使用mutate(case_when))
并使用lag()
。 然而,如果一个人错过了一轮研究,后来又回来了,它就会错误地工作。 数据集如下所示:
person | round | in_round |
A 1 1
A 2 1
A 3 1
A 4 1
A 5 1
A 6 0
A 7 0
A 8 0
B 1 0
B 2 0
B 3 1
B 4 1
B 5 1
B 6 1
B 7 0
B 8 1
我需要的是一个单独的列,它为每个人使用round
和in_round
来生成以下内容:
person | round | in_round | round_status
A 1 1 recruited
A 2 1 follow_up_1
A 3 1 follow_up_2
A 4 1 follow_up_3
A 5 1 follow_up_4
A 6 0 none
A 7 0 none
A 8 0 none
B 1 0 none
B 2 0 none
B 3 1 recruited
B 4 1 follow_up_1
B 5 1 follow_up_2
B 6 1 follow_up_3
B 7 0 none
B 8 1 follow_up_4
总之:
in_round == 0
, round_status == "none"
in_round == 1
, round_status == "recruited"
in_round == 1
, round_status == "follow_up_X"
(取决于个人所在的先前波数)。尝试这个:
df %>%
group_by(person) %>%
arrange(round) %>%
mutate(cum_round = cumsum(in_round),
round_status = case_when(
in_round == 0 ~ "none",
cum_round == 1 ~ "recruited",
TRUE ~ paste0("follow_up_", cum_round - 1)
))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.