1

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.

microliter
  • 13
  • 5
  • Welcome to SO! Instead of pasting the headers of each dataframe, please include the outputs of `dput(df1)` and `dput(df2)` to make this reproducible. – J.Con Jun 25 '17 at 04:08
  • Thanks for letting me know. I had a hard time formatting my dataframes for this post so this is helpful. – microliter Jun 25 '17 at 05:14

1 Answers1

1

I hope this code can help you:

ind <- match(names(df2), df1$mgi_symbol)
names(df2) <- df1$ensembl_gene_id[ind]
Shahab Einabadi
  • 307
  • 4
  • 15
  • This seemed to work with a slight modification: `ind <- match(colnames(df2), df1$mgi_symbol)` `colnames(df2) <- df1$ensembl_gene_id[ind]` – microliter Jun 25 '17 at 05:23
  • I think that if you have a dataframe both "names()" and "colnames()" functions return the same answer – Shahab Einabadi Jun 25 '17 at 15:32