Derived baseline flag, which is defined as the last non null test value before detection value. Here is SAS code,function first. and last. in SAS,is there a corresponding function in R?
data T001;
set aa;
if .<ady<=1 and ^missing(avalc) then flag=1;
run;
Proc sort data=T001;by usubjid paramn flag egdtc visitnum;run;
data T002;
set T001;
by usubjid paramn flag egdtc visitnum;
if last.flag and flag=1 then ablfl="Y";
run;
I also found some information, and I'm not sure if it's consistent
studyid usubjid param paramn avalc egdtc visitnum exstdtc ady ablfl
A A-S001 HR 1 10 2022/4/1 -1 2022/4/2 -1 Y
A A-S001 HR 1 11 2022/4/3 1 2022/4/2 2
A A-S001 HR 1 12 2022/4/6 2 2022/4/2 5
A A-S001 RESP 2 20 2022/3/31 -1 2022/4/2 -2
A A-S001 RESP 2 21 2022/4/1 1 2022/4/2 -1 Y
A A-S001 RESP 2 22 2022/4/3 2 2022/4/2 2
A A-S003 HR 1 11 2022/4/1 -1 2022/4/3 -2 Y
A A-S003 HR 1 2022/4/2 1 2022/4/3 -1
A A-S003 HR 1 12 2022/4/6 2 2022/4/3 4
A A-S003 RESP 2 21 2022/3/31 -1 2022/4/3 -3 Y
A A-S003 RESP 2 12 2022/4/4 1 2022/4/3 2
A A-S003 RESP 2 22 2022/4/6 2 2022/4/3 4
A A-S003 RESP 2 12 2022/4/8 3 2022/4/3 6
A A-S003 PLUS 3 44 2022/4/1 -1 2022/4/3 -2 Y
A A-S003 PLUS 3 44 2022/4/4 1 2022/4/3 2
A A-S003 PLUS 3 44 2022/4/6 2 2022/4/3 4
CodePudding user response:
I think you can simulate it using lead()/lag() functions.
Not the best R programmer, but something like this may get you started. If you make some fake data, we can test it.
T002 <- T001 %>%
rowwise()%>%
mutate(ablfl = case_when(usubjid == lag(usubjid) &
paramn == lag(paramn) &
flag != lead(flag) &
flag = 1
~ "Y"
TRUE ~ NA_character_))
CodePudding user response:
I worte this code, it looks similar to the meaning of this code, compared with SAS code
adeg <- aa %>%
mutate(FLAG = ifelse(ADY <= 1 & ADY>. & AVALC != "" , 1, NA)) %>%
arrange(STUDYID, USUBJID, PARAMCD, FLAG, EGDTC) %>%
group_by(USUBJID, USUBJID, PARAMCD, FLAG) %>%
mutate(ABLFL = ifelse(row_number() == max( row_number()) & FLAG == 1,"Y",NA))