Skip to contents

This function takes a mids object and returns a new mids object that pertains to the subset of the data identified by the expression in .... The expression may use column values from the incomplete data in .data$data.


# S3 method for mids
filter(.data, ..., .preserve = FALSE)



A mids object.


Expressions that return a logical value, and are defined in terms of the variables in .data$data. If multiple expressions are specified, they are combined with the & operator. Only rows for which all conditions evaluate to TRUE are kept.


Relevant when the .data input is grouped. If .preserve = FALSE (the default), the grouping structure is recalculated based on the resulting data, otherwise the grouping is kept as is.


An S3 object of class mids


The function calculates a logical vector include of length nrow(.data$data). The function constructs the elements of the filtered mids object as follows:

dataSelect rows in .data$data for which include == TRUE
impSelect rows each imputation data.frame in .data$imp for which include == TRUE
mEquals .data$m
whereSelect rows in .data$where for which include == TRUE
blocksEquals .data$blocks
callEquals .data$call
nmisRecalculate nmis based on the selected data rows
methodEquals .data$method
predictorMatrixEquals .data$predictorMatrix
visitSequenceEquals .data$visitSequence
formulasEquals .data$formulas
postEquals .data$post
blotsEquals .data$blots
ignoreSelect positions in .data$ignore for which include == TRUE
seedEquals .data$seed
iterationEquals .data$iteration
lastSeedValueEquals .data$lastSeedValue
chainMeanSet to NULL
chainVarSet to NULL
loggedEventsEquals .data$loggedEvents
versionReplaced with current version
dateReplaced with current date

See also


Patrick Rockenschaub


imp <- mice(nhanes, m = 2, maxit = 1, print = FALSE)

# example with external logical vector
imp_f <- filter(imp, c(rep(TRUE, 13), rep(FALSE, 12)))

#> [1] 25
#> [1] 13

# example with calculated include vector
imp_f2 <- filter(imp, age >= 2 & hyp == 1)
nrow(complete(imp_f2)) # should be 5
#> [1] 5