I have a matrix or a data-frame with specific column names. With a vector containing some of the column names, I can easily address those columns of the matrix. But is there as well a simple way to address the opposite columns, which are not listed in the vector:
mat <- matrix(c(1:12), ncol=4)
colnames(mat) <- c("a", "b", "c", "d")
not_target_col <- c("a", "b")
in this case I like to have the columns c
and d
.
I search something like this, without making the extra step:
pos <- colnames(mat) != not_target_col
mat[,pos]
Additional explanation
I like to make it more clear: When I have a numeric vector I can get the opposite when I add *-1
not_target_col <- c(1,2)
mat[,not_target_col * -1]
There is as well a technique like this when I use a logical vector. Here I just have to add a !
.
not_target_col <- c(T,T,F,F)
mat[,!not_target_col]