簡體   English   中英

使用 dplyr 基於 R 中的其他兩列自定義變異新列

[英]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

我需要的是一個單獨的列,它為每個人使用roundin_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 == 1round_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM