BioMan
BioMan

Reputation: 704

change column names based on matched names

I would like to replace the column names in dt2 by searching for matching names in col_name_dt1 then repplace it with what is in col_name_masked. I figured i could use the match commad somehow?

> col_name_dt1
   col_name col_name_masked
1      1_JH               1
2   107_MAE               4
3   108_IME               3
4    109_GW               2

> colnames(dt2)
 [1] "1_JH"     "107_MAE"  "108_IME"   "109_GW" 

Note: col_name_dt1 contains more names than is found in dt2. The length of dt2 is 62 whereas dim(col_name_dt1) is 96.

output

> colnames(dt2)
 [1] "1"     "4"  "3"   "2" 

Upvotes: 2

Views: 66

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

Try,

col_name_dt1$col_name_masked[match(colnames(dt2), col_name_dt1$col_name)]

#[1] 1 4 3 2

Upvotes: 1

Related Questions