I have DT1 and try to convert this table to DT2 with 1,0
DT1<-fread("variable bf_target_N bf_comparator_N
drugA 2 1
drugB 3 4
drugC 4 5
")
DT2<-fread("type drugA drugB drugC
bf_target_N 1 1 1
bf_target_N 1 1 1
bf_target_N 0 1 1
bf_target_N 0 0 1
bf_comparator_N 1 1 1
bf_comparator_N 0 1 1
bf_comparator_N 0 1 1
bf_comparator_N 0 1 1
bf_comparator_N 0 0 1
")
DT2 <- data.table(
type = c(rep("bf_target_N", nrow(DT1)), rep("bf_comparator_N", nrow(DT1))),
drugA = rep(0, 2 * nrow(DT1)),
drugB = rep(0, 2 * nrow(DT1)),
drugC = rep(0, 2 * nrow(DT1))
)
DT2[type == "bf_target_N", c("drugA", "drugB", "drugC") := .(DT1$bf_target_N, DT1$bf_target_N, DT1$bf_target_N)]
DT2[type == "bf_comparator_N", c("drugA", "drugB", "drugC") := .(DT1$bf_comparator_N, DT1$bf_comparator_N, DT1$bf_comparator_N)]
I tried some this way but fail to fill with 1,0