'Relabeling colnames within one matrix based on the colnames being present in a second matrix

I have two matrices A and B. Each are 38,000 x 10,000. I've combined A and B together by columns to make a matrix of 38,000 x 20,000. Each of the column names are unique and are formatted as follows: AACTACTCCGCTCTCA-1. Let's call the combined matrix AB.

Is it possible for me to use the colnames of A as a reference to change the matching colnames of AB to end with "^-A" rather than "^-1"?

I imagine this would be a for-loop but I can't quite piece together an r-script which works appropriately.

for (colnames(AB)) { if(colnames(AB) == colnames(A)){ str_replace(colnames(AB), "^-1", "^-A") } }



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source