Checking specific pair of codes within subject

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Checking specific pair of codes within subject

Frank S.
Dear all,
I come with an old question I had, which I have also solved but the last point. I've a data.table as follows:

dt <- data.table(id = rep(1:2, c(5, 2)),
     date = as.Date(rep(c("2005-07-25", "2006-09-17", "2001-10-19"), c(3, 2, 2))),
      sex = as.factor( rep(c(0, 1), c(5, 2))),
      hosp = as.factor(c('A11', 'C11', 'R1000', 'W1000', 'C11', 'B22', 'D22')),
      check = as.factor(rep(c(T, F, T), c(3, 2, 2))))

By avoiding splitting data table, I would like to do: If check = T, keep all rows by unique pairs (id, date) excepting those
where hosp %in% c("C11", "D22"). If check = F, keep all rows but changing "C11" to "A11". So desired otput is:

    id             date  sex  hosp
1:  1 2005-07-25   0   A11
2:  1 2005-07-25   0 R1000
3:  1 2006-09-17   0 W1000
4:  1 2006-09-17   0   A11
5:  2 2001-10-19   1   B22


I've tried (without result):

dtnew <- dt[, {
  if (check == TRUE) {
     idx <-  which(!hosp %in% c("C11", "D22"))
     res <- list(hosp = hosp[idx], sex = sex[idx], check = check[idx])
  } else {
     if (levels(hosp) == "C11"){
         idx <- which(id)
         res <- list(hosp = "A22", sex = sex[idx], check = check[idx])
     } else {
         idx <- which(id)
         res <- list(hosp = hosp[idx], sex = sex[idx], check = check[idx])
     }
  }
  res
}, by = list(id, date)]

I'm aware it may not be the most efficient way. Thanks for any help!!


Frank S.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.