How to dynamically access r matrix row names without explicitly calling the names?

152 views Asked by At

A few weeks ago I asked a question on how to extract certain rows from a matrix based on known criteria (How to extract rows with similar names into a submatrix?). The answers received worked phenomenally for the application at the time, but I find myself needing functionality to dynamically sample the row names and build the subsequent matrices without having to explicitly determine which name to extract.

For example, the model I built in the previous question has 6 row "families": Intercept, actsBreaks, tBreaks, etc. which could be replaced with 15+ other potential predictor variables. Changing the row construction sections by hand is cumbersome and something I need to avoid.

I messed around with readline(prompt=...) to have the user define which predictor variable family they wish to use, but was told this is a program the user needs to be able to loop overnight and walk away from, so no luck there. Is it possible to modify this code

uniBreakLines1 <- grep("actsBreaks[0-9]*", rows)
actsBreaks <- matrix1[uniBreakLines1,drop = FALSE]

to sample from the matrix row name vector without specifying exactly which "family" to use? Or should I use something else? The Caret Package looks promising but I am not well versed in r syntax and I am not sure if using something that claims to be primarily for machine learning would have this functionality. The answer in this question (Subsetting a matrix by row.names) utilizing the matrix indexing could have potential, but then we run into having to change the submatrix code by hand again.

TlDr: I need a way to filter rows by names from the matrix without typing out

uniBreakLines1 <- grep("actsBreaks[0-9]*", rows)
actsBreaks <- matrix1[uniBreakLines1,drop = FALSE]
1

There are 1 answers

1
Ben Bolker On BEST ANSWER

I'm not entirely sure I understand the question, but this will return a list of matrices, one for each category (I think):

res <- list()
brkcats <- unique(gsub("[0-9]*", "", rows))
for (bc in brkcats) {
      BreakLines <- grep(paste0(bc,"[0-9]*"), rows)
      res[[bc]] <- matrix1[BreakLines, , drop = FALSE]
}