Search code examples
rdataframerow

Swapping row names with a specific column in a dataframe


It would be useful for me to swap the rownames in this dataframe below with a specific column. In the first instance I'd like to swap the row names with the column hgnc_symbol, making hgnc_symbol the row names and the current row names column 8 (where hgnc_symbol is currently). This will make life much easier for me later on in my script.

DataFrame with 16480 rows and 8 columns

                        baseMean     log2FoldChange             lfcSE               stat               pvalue                 padj  hgnc_symbol    entrezid
                       <numeric>          <numeric>         <numeric>          <numeric>            <numeric>            <numeric>  <character> <character>
ENSG00000223972 4.51685289775514   2.15550731621112  1.43343074902207   1.50374011279002    0.132648296504179    0.292089709671559      DDX11L1   100287102
ENSG00000278267 3.52926357135542 0.0672084810098236   1.8687740950403 0.0359639408466726    0.971311111378278    0.984802707352042    MIR6859-1   102466751
ENSG00000230021 34.7527277878475   3.18376204618685 0.577139078176496   5.51645550713033 3.45905009734984e-08 2.68321826693434e-06 LOC101928626   101928626
ENSG00000177757  13.254684151203 -0.355758563780635 0.566844518813169 -0.627612249873219    0.530257981962198    0.706449586670417       FAM87B      400728
ENSG00000228794 339.345230342994  0.440735348198774 0.225273115379179   1.95644894179641   0.0504122915707138    0.153177028381987    LINC01128      643837
...                          ...                ...               ...                ...                  ...                  ...          ...         ...
ENSG00000212907 13316.6212444405  -1.29749484848357 0.734627626363388  -1.76619392181932   0.0773633204369255    0.204515519463446         ND4L        4539
ENSG00000198886 202122.249106362  0.102983861749155 0.633779243148049  0.162491692276988    0.870918669941202    0.931706464999579          ND4        4538
ENSG00000198786 117214.179395624  0.365274513410682 0.651430081099186  0.560727120237277    0.574983576113379    0.741572320729196          ND5        4540
ENSG00000198695 25719.2572032967   0.17861898817241 0.479782318460458  0.372291727518365    0.709675657170189    0.834502165470665          ND6        4541
ENSG00000198727 117114.965672949  0.140668405549489 0.564196791170924  0.249325071944399    0.803109338951998    0.891399787624092         CYTB        4519

I did exhaustively look for a solution for this on stack overflow and other forums but was unable to get a one step answer.

Any help would be greatly appreciated.

Seasons greetings,

Jas


Solution

  • One dplyr and tibble option could be:

    df %>%
     rownames_to_column() %>%
     column_to_rownames("hgnc_symbol") %>%
     rename(hgnc_symbol = "rowname")
    
                     hgnc_symbol     baseMean log2FoldChange     lfcSE        stat       pvalue         padj  entrezid
    DDX11L1      ENSG00000223972 4.516853e+00     2.15550732 1.4334307  1.50374011 1.326483e-01 2.920897e-01 100287102
    MIR6859-1    ENSG00000278267 3.529264e+00     0.06720848 1.8687741  0.03596394 9.713111e-01 9.848027e-01 102466751
    LOC101928626 ENSG00000230021 3.475273e+01     3.18376205 0.5771391  5.51645551 3.459050e-08 2.683218e-06 101928626
    FAM87B       ENSG00000177757 1.325468e+01    -0.35575856 0.5668445 -0.62761225 5.302580e-01 7.064496e-01    400728
    LINC01128    ENSG00000228794 3.393452e+02     0.44073535 0.2252731  1.95644894 5.041229e-02 1.531770e-01    643837
    ND4L         ENSG00000212907 1.331662e+04    -1.29749485 0.7346276 -1.76619392 7.736332e-02 2.045155e-01      4539
    ND4          ENSG00000198886 2.021222e+05     0.10298386 0.6337792  0.16249169 8.709187e-01 9.317065e-01      4538
    ND5          ENSG00000198786 1.172142e+05     0.36527451 0.6514301  0.56072712 5.749836e-01 7.415723e-01      4540
    ND6          ENSG00000198695 2.571926e+04     0.17861899 0.4797823  0.37229173 7.096757e-01 8.345022e-01      4541
    CYTB         ENSG00000198727 1.171150e+05     0.14066841 0.5641968  0.24932507 8.031093e-01 8.913998e-01      4519