Skip to content

Subsetting problem data, 2

2 messages · Lib Gray, Rui Barradas

#
Hello,

Try the following. The data is your example of Patient A through E, but 
from the output of dput().

dat <- structure(list(Patient = structure(c(1L, 1L, 1L, 1L, 1L, 2L,
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L), .Label = c("A",
"B", "C", "D", "E"), class = "factor"), Cycle = c(1L, 2L, 3L,
4L, 5L, 1L, 2L, 1L, 3L, 4L, 5L, 1L, 2L, 4L, 5L, 1L, 2L, 3L),
     V1 = c(0.4, 0.3, 0.3, 0.4, 0.5, 0.4, 0.4, 0.9, 0.3, NA, 0.4,
     0.2, 0.5, 0.6, 0.5, 0.1, 0.5, 0.4), V2 = c(0.1, 0.2, NA,
     NA, 0.2, NA, NA, 0.9, 0.5, NA, NA, 0.5, 0.7, 0.4, 0.5, NA,
     0.3, 0.3), V3 = c(0.5, 0.5, 0.6, 0.4, 0.5, NA, NA, 0.9, 0.6,
     NA, NA, NA, NA, NA, NA, NA, NA, NA), V4 = c(1.5, 1.6, 1.7,
     1.8, 1.5, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
     NA), V5 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
     NA, NA, NA, NA, NA, NA)), .Names = c("Patient", "Cycle",
"V1", "V2", "V3", "V4", "V5"), class = "data.frame", row.names = c(NA,
-18L))

dat

nms <- names(dat)[grep("^V[1-9]$", names(dat))]
dd <- split(dat, dat$Patient)
fun <- function(x) any(is.na(x)) && any(!is.na(x))
ix <- sapply(dd, function(x) Reduce(`|`, lapply(x[, nms], fun)))

dd[ix]
do.call(rbind, dd[ix])


I'm assuming that the variables names are as posted, V followed by one 
single digit 1-9. To keep the Patients with complete cases just negate 
the index 'ix', it's a logical index.
Note also that dput() is the best way of posting a data example.

Hope this helps,

Rui Barradas

Em 19-07-2012 15:15, Lib Gray escreveu: