amt <- list()   # Empty list to store the output
for (i in 1:pnum) {
  # Check if the subject has dose amount changes before
  amt_tmp <- c()
  for (j in 1:(nrow(sub_trt2))) {
    if (namt[i] == 1) {
      amt_tmp <- c(rep(sub_trt2$dose[[j]], sub_trt2$No_TRT[[j]]))   ##no dose changed
    } else if (namt[i] != 1 & namt[i] > j & namt[i] == 2) {
      amt_tmp <- c(amt_tmp, rep(sub_trt2$dose[[j]], sub_trt2$No_TRT[[j]]),
                   rep(sub_trt2$dose[[j+1]], sub_trt2$No_TRT[[j+1]]))
    }
    else if (namt[i] != 1 & namt[i] > j & namt[i] == 3) {
      amt_tmp <- c(rep(sub_trt2$dose[[j]], sub_trt2$No_TRT[[j]]),
                   rep(sub_trt2$dose[[j+1]], sub_trt2$No_TRT[[j+1]]), rep(sub_trt2$dose[[j+2]], sub_trt2$No_TRT[[j+2]]))
    }
    else if (namt[i] != 1 & namt[i] == j) {
      amt_tmp <- c(rep(sub_trt2$dose[(nrow(sub_trt2)-i+1)], sub_trt2$No_TRT[(nrow(sub_trt2)-i+1)]),
                   rep(sub_trt2$dose[(nrow(sub_trt2)-i+2)], sub_trt2$No_TRT[(nrow(sub_trt2)-i+2)]))
    }
  }Error in rep(sub_trt2$dose[(nrow(sub_trt2) - i + 2)], sub_trt2$No_TRT[(nrow(sub_trt2) -  : 
  'times'参数不对  how to fix it 
  amt[[i]] <- amt_tmp
}