john james
2010-Jun-01 04:13 UTC
[R] selecting monotone pattern of missing data from a dataframe with mixed pattern of missingness
Dear R- User, I have a dataset that looks like the following: jh<-data.frame( 'id'=seq(1,10,1), 'time0'=c(8,5,8,8,9,NA,NA,2,4,5), 'time4'=c(NA,NA,9,8,NA,2,3,2,4,5), 'time8'=c(NA,2,8,NA,5,NA,2,3,NA,4), 'time12'=c(NA,2,NA,NA,NA,3,3,2,3,NA), 'sex'=c('m','f','m','m','f','f','f','m','f','m'), 'trt'=c('P','T','P','P','T','P','T','P','P','T') ) I was earlier furnished (from R-help) with the function below: f <- function (x) { stopifnot(is.data.frame(x)) o <- do.call(order, c(list(rowSums(is.na(x))), lapply(x[, order(-sapply(x, function(x) sum(is.na(x))))], function(x) is.na(x)))) xo <- x[o, , drop = FALSE] isNonterminalNA <- function(x) is.na(x) & rev(cummax(!is.na(rev(x))) > 0) good <- rep(TRUE, nrow(x)) for (j in seq(along = x)) { good <- good & !isNonterminalNA(xo[, j, drop = TRUE]) } xo[good, , drop = FALSE] } The function works well when the measurement occassions are just 3. When the measurement occassion becomes 4, I observed that pattern X X X NA is exclude, which is also a monotone pattern. Please how do i adjust a function like this to work in all cases. i.e to select only monotone patterns: jhmonotone<-data.frame( 'id'=c(8,3,10,4,1), 'time0'=c(2,8,5,8,8), 'time4'=c(2,9,5,8,NA), 'time8'=c(3,8,4,NA,NA), 'time12'=c(2,NA,NA,NA,NA), 'sex'=c('m','m','m','m','m'), 'trt'=c('P','P','T','P','P') ) Many thanks in advance John [[alternative HTML version deleted]]