# [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')
)
ĀĀ
Ā

Ā

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.