-1

I am trying to create a new integer variable AE (1 or 0).

I have three flags (AEACN12, AEACN13, AEACN) per patient. The dataset has multiple records per patient. If at least one of the three flags is equal to "DRUG WITHDRAWN", I would like the flag only that record with AE=1. The flags can have other values or missing. If none of the three flags equals "DRUG WITHDRAWN", then AE=0.

I am using data.table approach but the code below is not producing what I need. Can you help?

AE <- adae[SAFFL=="Y"][AEREL=="Y" & AETRTFL=="Y", `:=`(AE=fifelse(any(AEACN12=="DRUG WITHDRAWN", AEACN13=="DRUG WITHDRAWN", AEACN=="DRUG WITHDRAWN"), 1, 0))][order(USUBJID)][,.(USUBJID,AEACN12,AEACN13,AEACN, AE, SAFFL, AEREL, AETRTFL)]

MKro
  • 11
  • 3
  • You will continue to have difficulty in getting correct and timely answers suggested until you provide sample data and the expected output given that data. Please see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info for commentary on the use of `read.table(.)` or `dput(.)` to share unambiguous, representative sample data. – r2evans Mar 17 '22 at 13:08

1 Answers1

0

Perhaps something like this:

AE <- adae[
  ,AE:=fifelse(any(AEACN12=="DRUG WITHDRAWN", AEACN13=="DRUG WITHDRAWN", EACN=="DRUG WITHDRAWN"),1,0),
  by=1:nrow(adae)][SAFFL=="Y" & AEREL=="Y" & AETRTFL=="Y"]

OR

AE <- adae[SAFFL=="Y" & AEREL=="Y" & AETRTFL=="Y"][
  ,AE:=fifelse(any(AEACN12=="DRUG WITHDRAWN", AEACN13=="DRUG WITHDRAWN", EACN=="DRUG WITHDRAWN"),1,0),
  ,by=.I]
langtang
  • 11,276
  • 10
  • 25
  • Hello, thank you for your input. The provided code does not work still. The flags can have missing values. I have edited the question slightly. – MKro Mar 17 '22 at 00:52
  • sorry that was my best shot without seeing your data or your expected output. Try providing a reproducible example, the error messages you are seeing (if any), and your desired output – langtang Mar 17 '22 at 01:17
  • also, you state you are trying to create a variable called `AE`, which i can see you trying to create, but you also assigning the resulting datatable to a new object, also called `AE`. Is that your intent? `:=` assigns by reference, there is generally no need to assign the data.table to a new object, if you are trying to simply add a new variable. – langtang Mar 17 '22 at 01:23
  • Hi, I actually managed to figure it out, although the solution is not elegant. This works: `DT – MKro Mar 17 '22 at 02:03