0

我有一个从 REDCap 中提取的数据框,我已经在 RStudio 中导入了 CSV。列出唯一的参与者 ID,然后列出事件。我需要列出每个重复事件的数量,例如,acute_event_infect_arm_4 表示 1010002 是第一个拒绝事件,然后 _4b 是第二个事件,_4c 是第三个,依此类推。

我需要为跟进、拒绝和 CMV/EBV 事件执行此操作

这是具有 3 个 id 的数据帧的小快照(实际 df 有 1000 个 id)

在此处输入图像描述

structure(list(id = c(1010002, 1010002, 1010002, 1010002, 1010002, 
1010002, 1010002, 1010002, 1010002, 1010002, 1010002, 1010002, 
1010002, 1010002, 1010002, 1010002, 1010006, 1010006, 1010006, 
1010006, 1010006, 1010006, 1010006, 1010006, 1010006, 1010006, 
1010006, 1010008, 1010008, 1010008, 1010008, 1010008, 1010008, 
1010008, 1010008, 1010008, 1010008, 1010008), redcap_event_name = 
c("pre_transplant_arm_4", 
"transplant_arm_4", "transplant_2_arm_4", "end_of_followup_fo_arm_4", 
"last_encounter_arm_4", "acute_event_reject_arm_4", 
"acute_event_reject_arm_4b", 
"acute_event_infect_arm_4", "acute_event_infect_arm_4b", 
"acute_event_infect_arm_4c", 
"acute_event_infect_arm_4d", "acute_event_infect_arm_4e", 
"acute_event_infect_arm_4f", 
"acute_event_infect_arm_4g", "acute_event_cmvebv_arm_4", 
"acute_event_cmvebv_arm_4b", 
"pre_transplant_arm_4", "transplant_arm_4", "1_month_followup_arm_4", 
"2_year_followup_arm_4", "last_encounter_arm_4", "acute_event_reject_arm_4", 
"acute_event_reject_arm_4b", "acute_event_infect_arm_4", 
"acute_event_infect_arm_4b", 
"acute_event_infect_arm_4c", "acute_event_cmvebv_arm_4", 
"pre_transplant_arm_4", 
"transplant_arm_4", "3_month_followup_arm_4", "6_month_followup_arm_4", 
"1_year_followup_arm_4", "2_year_followup_arm_4", "3_year_followup_arm_4", 
"last_encounter_arm_4", "acute_event_reject_arm_4", 
"acute_event_infect_arm_4", 
"acute_event_cmvebv_arm_4")), row.names = c(NA, -38L), class = c("tbl_df", 
"tbl", "data.frame"))

这是我需要在 redcap_repeat 列中添加的内容

在此处输入图像描述

@akrun 请在下面提供一些示例(缺少粗体红色)

在此处输入图像描述

4

1 回答 1

1

这是一种选择

library(tidyverse)
df1 %>% 
    group_by(id, grp1 = str_remove(redcap_event_name, "[a-z]$|^\\d+_")) %>% 
    mutate(redcap_repeat =if(any(str_detect(redcap_event_name,  "[a-z]$|^[0-9]")) & 
       n() > 1) as.character(row_number()) else "") %>% 
     ungroup %>%
     group_by(id, grp1 = str_remove(redcap_event_name, "^\\d+_(month|year)_")) %>%
     mutate(redcap_repeat = case_when(redcap_repeat != "" & n() > 1 ~ 
          as.character(row_number()),
          TRUE ~ redcap_repeat)) %>% 
     ungroup %>%
     select(-grp1) %>%
     as.data.frame

-输出

#        id         redcap_event_name redcap_repeat
#1  1010002      pre_transplant_arm_4              
#2  1010002          transplant_arm_4              
#3  1010002        transplant_2_arm_4              
#4  1010002  end_of_followup_fo_arm_4              
#5  1010002      last_encounter_arm_4              
#6  1010002  acute_event_reject_arm_4             1
#7  1010002 acute_event_reject_arm_4b             2
#8  1010002  acute_event_infect_arm_4             1
#9  1010002 acute_event_infect_arm_4b             2
#10 1010002 acute_event_infect_arm_4c             3
#11 1010002 acute_event_infect_arm_4d             4
#12 1010002 acute_event_infect_arm_4e             5
#13 1010002 acute_event_infect_arm_4f             6
#14 1010002 acute_event_infect_arm_4g             7
#15 1010002  acute_event_cmvebv_arm_4             1
#16 1010002 acute_event_cmvebv_arm_4b             2
#17 1010006      pre_transplant_arm_4              
#18 1010006          transplant_arm_4              
#19 1010006    1_month_followup_arm_4              
#20 1010006     2_year_followup_arm_4              
#21 1010006      last_encounter_arm_4              
#22 1010006  acute_event_reject_arm_4             1
#23 1010006 acute_event_reject_arm_4b             2
#24 1010006  acute_event_infect_arm_4             1
#25 1010006 acute_event_infect_arm_4b             2
#26 1010006 acute_event_infect_arm_4c             3
#27 1010006  acute_event_cmvebv_arm_4              
#28 1010008      pre_transplant_arm_4              
#29 1010008          transplant_arm_4              
#30 1010008    3_month_followup_arm_4             1
#31 1010008    6_month_followup_arm_4             2
#32 1010008     1_year_followup_arm_4             3
#33 1010008     2_year_followup_arm_4             4
#34 1010008     3_year_followup_arm_4             5
#35 1010008      last_encounter_arm_4              
#36 1010008  acute_event_reject_arm_4              
#37 1010008  acute_event_infect_arm_4              
#38 1010008  acute_event_cmvebv_arm_4              
于 2019-05-24T19:50:17.110 回答