I am wondering if there is a way to quickly and efficiently filter matrices without having to convert them into a data frame or data.table.
Please consider the following matrix:
# Choose control variables
control_vars <- c("x1", "F_5", "F_7")
my_args = lapply(control_vars, function(x) c("", x))
# Create a matrix of all combinations of the control variables
all_combs = as.matrix(do.call("expand.grid", my_args))
The output looks like this:
Var1 Var2 Var3
[1,] "" "" ""
[2,] "x1" "" ""
[3,] "" "F_5" ""
[4,] "x1" "F_5" ""
[5,] "" "" "F_7"
[6,] "x1" "" "F_7"
[7,] "" "F_5" "F_7"
[8,] "x1" "F_5" "F_7"
I want to eliminate any row in this matrix where both F_5 and F_7 are present. This would be row 7 and 8.
For some context, I am trying to run stepwise regressions and want to eliminate rows where there are "redundant" fixed effects. That is, I only need F_5 or F_7, but not both. For example, suppose that F_5 and F_7 that categorize countries into 5 and 7 groups. The latter is more granular than the former, but I don't want them both present in my regression.
This would be easy to do after converting it into a data.frame by counting the number of "F"s in a row and then deleting them, but I am wondering if there is a faster and cleaner solution while using matrices. My real dataset has 8 different substitutable fixed effects, so manually deleting them is also not possible.
Thanks!