DoeNoe
DoeNoe

Reputation: 93

Extract Columns that Do Not Exist in Another Matrix Based on Column Names

I have two matrices df_matrix and df_subset. One is a subset of the other one. Therefore, df_matrix has 10000 rows and columns and df_subset contains only 8222 columns and rows of df_matrix.

I want to select only those columns from df_matrix that are NOT in df_subset. I thought it is best to do it by column names, so I tried executing this code:

newdf <- df_matrix[, which( (colnames(df_matrix)) != (colnames(KroneckerProducts)) )]

However, this is not working at all. Is there any other way to do this?

Upvotes: 0

Views: 127

Answers (1)

vasanthcullen
vasanthcullen

Reputation: 386

General rule is not to use == or != with objects of different length

Use %in% with !

newdf <- df_matrix[, !(colnames(df_matrix) %in% colnames(KroneckerProducts))]

Upvotes: 4

Related Questions