library(data.table)
dt.original.data <- structure(
list(
msg_seq_nb = c("0005747", "0005747", "0005765", "0005765", "0005783", "0005783", "0008333", "0008333", "0008494",
"0008494", "0008556", "0008556", "0008560", "0008560", "0013622", "0013622", "0013797", "0013797", "0024541", "0024541", "0024569"
), orig_msg_seq_nb = c(NA, NA, "0005747", "0005747", "0005765", "0005765", "0005783", "0005783", "0008333", "0008333", "0008494",
"0008494", "0008556", "0008556", "0008560", "0008560", "0013622", "0013622", "0013797", "0013797", "0024541")
, trc_st = c("T","C", "R", "C", "R", "C", "R", "C", "R", "C", "R", "C", "R", "C", "R", "C", "R", "C", "R", "C", "R")
, trd_rpt_dt = structure(c(15987, 15987, 15987, 15987, 15987, 15987, 15987, 15987, 15987, 15987, 15987, 15987, 15987, 15987, 15987, 15987, 15987, 15987, 15987, 15987, 15987), class = "Date")
, trd_rpt_tm = c(34838, 34853, 34853, 34863, 34863, 36231, 36231, 36305, 36305, 36328, 36328, 36330, 36330, 38831, 38831, 38925, 38925, 42984, 42984, 43002, 43002))
, row.names = c(NA, -21L), class = c("data.table", "data.frame"))
final_transition <- dt.original.data[trc_st != "T" & orig_msg_seq_nb %in% dt.original.data[trc_st == "T"]$msg_seq_nb]
final_transition <- merge(dt.original.data[trc_st == "T", c("trc_st", "trd_rpt_tm", "msg_seq_nb")], final_transition, by.x = "msg_seq_nb", by.y = "orig_msg_seq_nb")
col_names <- c("msg_seq_nb", "Initial.Trade.Status", "Initial.Trd.Rpt.Dt", "Initial.Trd.Rpt.Tm", "J2.Msg.Nb", "J2.Trade.Status", "J2.Trd.Rpt.Tm")
setnames(final_transition,
c("msg_seq_nb", "trc_st.x", "trd_rpt_dt", "trd_rpt_tm.x", "msg_seq_nb.y", "trc_st.y", "trd_rpt_tm.y"),
col_names)
setcolorder(final_transition, col_names)
final_transition