Remove columns in a matrix that have a match with the column name in R <p>I have a matrix in a R script and i want to remove columns in a matrix in R that have a match with the label.</p> <p>For example :</p> <pre><code>A &lt;- matrix(c(4,5,4,4), nrow=1) dimnames(A)= list(c("row1"),c("foo","bar","alfa","foo")) foo bar alfa foo row1 4 5 4 4 </code></pre> <p>I want remove the column foo-4 because match with the label but not the column alfa-4</p> <p>I try</p> <pre><code>duplicated.columns &lt;- duplicated(t(A)) A &lt;- A[, !duplicated.columns] </code></pre> <p>but the result is </p> <pre><code>foo bar 4 5 </code></pre> <p>How can I fix this?</p>