rbioinformaticsbiomart

Replacing column names in one data frame with rows from another data frame if adjacent row matches


I am trying to replace the column names of one data frame with the values in another data frame if the adjacent row in that data frame matches.

head(df1)

ensembl_gene_id     mgi_symbol
ENSMUSG00000021252  0610007P14Rik
ENSMUSG00000007777  0610009B22Rik
ENSMUSG00000086714  0610009E02Rik
ENSMUSG00000024442  0610009O20Rik
ENSMUSG00000042208  0610010F05Rik
ENSMUSG00000058706  0610030E20Rik

head(df2)

0610007N19Rik   0610007P14Rik   0610009B22Rik   0610009D07Rik   0610009E02Rik   0610009O20Rik   0610010F05Rik   0610011F06Rik   0610030E20Rik   0610031J06Rik   ⋯   mt-Tl1  mt-Tm   mt-Tp   mt-Tq   mt-Ts2  mt-Tv   3110079O15Rik   4933408B17Rik   Efcab9  Il17rd
GTCATCTTTACT.AJP1   -0.07497236 -0.1947123  -0.1656026  -0.252564   -0.08834634 5.2688983   -0.09093407 -0.1779879  -0.15511    1.6949233   ⋯   -0.02246199 -0.04942127 -0.09752107 -0.03143053 -0.03057371 -0.05273995 -0.02246199 -0.03175767 -0.02246199 -0.0824498
CCGACGTATCGT.AJP1   -0.07497236 -0.1947123  -0.1656026  -0.252564   -0.08834634 -0.1534577  -0.09093407 2.9506885   -0.15511    -0.2636754  ⋯   -0.02246199 -0.04942127 -0.09752107 -0.03143053 -0.03057371 -0.05273995 -0.02246199 -0.03175767 -0.02246199 -0.0824498
TTGGTACTTCCG.AJP1   -0.07497236 -0.1947123  -0.1656026  -0.252564   -0.08834634 -0.1534577  -0.09093407 -0.1779879  -0.15511    -0.2636754  ⋯   -0.02246199 -0.04942127 -0.09752107 -0.03143053 -0.03057371 -0.05273995 -0.02246199 -0.03175767 -0.02246199 -0.0824498
AAGAGCGCGTGC.AJP1   -0.07497236 -0.1947123  -0.1656026  -0.252564   -0.08834634 -0.1534577  -0.09093407 -0.1779879  -0.15511    -0.2636754  ⋯   -0.02246199 -0.04942127 -0.09752107 -0.03143053 -0.03057371 -0.05273995 -0.02246199 -0.03175767 -0.02246199 -0.0824498
GCTATCTTCCTN.AJP1   -0.07497236 -0.1947123  -0.1656026  -0.252564   -0.08834634 -0.1534577  -0.09093407 -0.1779879  -0.15511    -0.2636754  ⋯   -0.02246199 -0.04942127 -0.09752107 -0.03143053 -0.03057371 -0.05273995 -0.02246199 -0.03175767 -0.02246199 -0.0824498

In the example provided above, I would like to replace the column names in df2 with the matching value from df1$ensembl_gene_id.

nrow(df1) is greater than ncol(df2) and there are non-matching values so I can't simply replace colnames(df1) with df1$ensembl_gene_id

This is probably a relatively-simple data-wrangling problem, but I can't seem to figure it out. Any help would be appreciated.


Solution

  • I hope this code can help you:

    ind <- match(names(df2), df1$mgi_symbol)
    names(df2) <- df1$ensembl_gene_id[ind]