Skip to content

Instantly share code, notes, and snippets.

@jonesor
Last active July 18, 2026 20:12
Show Gist options
  • Select an option

  • Save jonesor/cd1176e4ce5104f8869531ad99add368 to your computer and use it in GitHub Desktop.

Select an option

Save jonesor/cd1176e4ce5104f8869531ad99add368 to your computer and use it in GitHub Desktop.
How to subset the COMPADRE/COMADRE matrix database based on the matrices themselves. This example counts NA values in the F matrix and uses that to subset.
# How to subset the matrix database based on the matrices themselves.
# This example counts NA values in the F (fecundity) matrix of each population
# and uses that to subset. Uses the modern Rcompadre CompadreDB API.
library(Rcompadre)
compadre <- cdb_fetch("compadre")
# matF() returns a list of the F matrices, one per row of the database.
# Count the NA values in each, and store as a new metadata column.
compadre$NAinFmat <- sapply(matF(compadre), function(m) sum(is.na(m)))
table(compadre$NAinFmat)
# Keep only populations whose F matrix has no NA values.
x <- subset(compadre, NAinFmat == 0)
table(x$NAinFmat)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment