[R] selecting monotone pattern of missing data from a dataframe with mixed pattern of missingness

From: john james <dntssa_at_yahoo.com>
Date: Mon, 31 May 2010 21:13:34 -0700 (PDT)


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]]


______________________________________________

R-help_at_r-project.org mailing list
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. Received on Tue 01 Jun 2010 - 05:25:21 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 01 Jun 2010 - 05:40:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive