Home > Back-end >  Shifting column titles (row 1) to the right
Shifting column titles (row 1) to the right

Time:01-08

I am exporting a data frame, but the generated column titles are not on the correct data entries. I'd like to shift all of the current columns over by 1 to the right, and add a title to column 1.

Example of current output:

baseMean    log2FoldChange  lfcSE   stat    pvalue  padj    
ENSMUST00000103410.3    15237.92545 1.667016621 0.472605404 3.527290643 0.000419836 0.00977886

What I want

transcript_ID   baseMean    log2FoldChange  lfcSE   stat    pvalue  padj    
ENSMUST00000103410.3    15237.92545 1.667016621 0.472605404 3.527290643 0.000419836 0.00977886

Here is what I used to export my data frame:

write.table(resSig, file = outFile, sep = "\t", eol = "\n")

This is what the original data frame looked like using dput(head(resSig))

new("DESeqResults", priorInfo = list(), rownames = c("ENSMUST00000103410.3", 
"ENSMUST00000194738.6", "ENSMUST00000178282.3", "ENSMUST00000200568.2", 
"ENSMUST00000196768.2", "ENSMUST00000197515.2"), nrows = 6L, 
    elementType = "ANY", elementMetadata = new("DFrame", rownames = NULL, 
        nrows = 6L, elementType = "ANY", elementMetadata = NULL, 
        metadata = list(), listData = list(type = c("intermediate", 
        "results", "results", "results", "results", "results"
        ), description = c("mean of normalized counts for all samples", 
        "log2 fold change (MLE): group 02 Knockout vs 01 Wildtype", 
        "standard error: group 02 Knockout vs 01 Wildtype", "Wald statistic: group 02 Knockout vs 01 Wildtype", 
        "Wald test p-value: group 02 Knockout vs 01 Wildtype", 
        "BH adjusted p-values"))), metadata = list(filterThreshold = c(`17.59868%` = 16.7718228975726), 
        filterTheta = 0.175986831193112, filterNumRej = structure(list(
            theta = c(0.00183386821156171, 0.0211841974317339, 
            0.0405345266519061, 0.0598848558720783, 0.0792351850922505, 
            0.0985855143124228, 0.117935843532595, 0.137286172752767, 
            0.156636501972939, 0.175986831193112, 0.195337160413284, 
            0.214687489633456, 0.234037818853628, 0.2533881480738, 
            0.272738477293973, 0.292088806514145, 0.311439135734317, 
            0.330789464954489, 0.350139794174662, 0.369490123394834, 
            0.388840452615006, 0.408190781835178, 0.42754111105535, 
            0.446891440275523, 0.466241769495695, 0.485592098715867, 
            0.504942427936039, 0.524292757156211, 0.543643086376384, 
            0.562993415596556, 0.582343744816728, 0.6016940740369, 
            0.621044403257072, 0.640394732477245, 0.659745061697417, 
            0.679095390917589, 0.698445720137761, 0.717796049357934, 
            0.737146378578106, 0.756496707798278, 0.77584703701845, 
            0.795197366238622, 0.814547695458795, 0.833898024678967, 
            0.853248353899139, 0.872598683119311, 0.891949012339483, 
            0.911299341559656, 0.930649670779828, 0.95), numRej = c(5345, 
            5347, 5359, 5388, 5404, 5408, 5443, 5462, 5483, 5503, 
            5522, 5515, 5500, 5493, 5516, 5530, 5496, 5490, 5480, 
            5459, 5440, 5423, 5379, 5312, 5269, 5196, 5164, 5108, 
            5048, 4946, 4835, 4699, 4566, 4438, 4277, 4104, 3936, 
            3763, 3602, 3387, 3189, 2959, 2700, 2487, 2242, 1963, 
            1695, 1397, 1122, 883)), class = "data.frame", row.names = c("0.1833868%", 
        "2.11842%", "4.053453%", "5.988486%", "7.923519%", "9.858551%", 
        "11.79358%", "13.72862%", "15.66365%", "17.59868%", "19.53372%", 
        "21.46875%", "23.40378%", "25.33881%", "27.27385%", "29.20888%", 
        "31.14391%", "33.07895%", "35.01398%", "36.94901%", "38.88405%", 
        "40.81908%", "42.75411%", "44.68914%", "46.62418%", "48.55921%", 
        "50.49424%", "52.42928%", "54.36431%", "56.29934%", "58.23437%", 
        "60.16941%", "62.10444%", "64.03947%", "65.97451%", "67.90954%", 
        "69.84457%", "71.7796%", "73.71464%", "75.64967%", "77.5847%", 
        "79.51974%", "81.45477%", "83.3898%", "85.32484%", "87.25987%", 
        "89.1949%", "91.12993%", "93.06497%", "95%")), lo.fit = list(
            x = c(0.00183386821156171, 0.0211841974317339, 0.0405345266519061, 
            0.0598848558720783, 0.0792351850922505, 0.0985855143124228, 
            0.117935843532595, 0.137286172752767, 0.156636501972939, 
            0.175986831193112, 0.195337160413284, 0.214687489633456, 
            0.234037818853628, 0.2533881480738, 0.272738477293973, 
            0.292088806514145, 0.311439135734317, 0.330789464954489, 
            0.350139794174662, 0.369490123394834, 0.388840452615006, 
            0.408190781835178, 0.42754111105535, 0.446891440275523, 
            0.466241769495695, 0.485592098715867, 0.504942427936039, 
            0.524292757156211, 0.543643086376384, 0.562993415596556, 
            0.582343744816728, 0.6016940740369, 0.621044403257072, 
            0.640394732477245, 0.659745061697417, 0.679095390917589, 
            0.698445720137761, 0.717796049357934, 0.737146378578106, 
            0.756496707798278, 0.77584703701845, 0.795197366238622, 
            0.814547695458795, 0.833898024678967, 0.853248353899139, 
            0.872598683119311, 0.891949012339483, 0.911299341559656, 
            0.930649670779828, 0.95), y = c(5335.46131849384, 
            5351.75931963814, 5368.24006541122, 5384.82625509139, 
            5401.74096358571, 5421.02191012546, 5440.87449724393, 
            5460.32340221543, 5477.70913137891, 5490.8460392606, 
            5499.23496889586, 5504.96951560283, 5508.44352637341, 
            5508.35788315921, 5505.45460585055, 5501.44760122436, 
            5495.95338115932, 5486.83826794266, 5472.79579642974, 
            5453.28317378305, 5427.24196153897, 5394.65455650187, 
            5355.75204143892, 5311.7867535417, 5262.82329137616, 
            5209.45145522153, 5149.36948769144, 5076.6561650459, 
            4993.07570722587, 4902.79783781246, 4803.90833733924, 
            4689.90607991795, 4555.92950048918, 4409.65668444394, 
            4256.77751387084, 4097.77077356365, 3929.65636215747, 
            3751.15196287731, 3561.34895352574, 3361.64177451808, 
            3154.57771628599, 2937.02005661935, 2705.04788342637, 
            2461.08101990244, 2207.15659031582, 1946.55194836711, 
            1679.1786798783, 1411.34786747145, 1142.36760346868, 
            872.547319046507)), alpha = 0.1, lfcThreshold = 0), 
    listData = list(baseMean = c(15237.9254502146, 134.444977784854, 
    24953.2772927054, 675.407908381536, 69.8793042837061, 119.636634328027
    ), log2FoldChange = c(1.66701662080992, 1.90957677023213, 
    1.52327192680189, 2.28454416963816, 2.99795529787604, 3.17069759247583
    ), lfcSE = c(0.47260540440168, 0.391318814751557, 0.360111746350888, 
    0.557793677905659, 0.872258801871395, 0.782073423875039), 
        stat = c(3.52729064306908, 4.87984911087011, 4.22999788881542, 
        4.09567956778554, 3.43700205884315, 4.05421984136167), 
        pvalue = c(0.000419835632090353, 1.06167035104576e-06, 
        2.33693505074461e-05, 4.20931810845247e-05, 0.000588191132477059, 
        5.03018945093179e-05), padj = c(0.00977885969583928, 
        0.000155170621717427, 0.00133475920575386, 0.00201381009038859, 
        0.0122055138086285, 0.00229394288750037))) 

I tried:

```r
names(resSig)[1] <- "transcript_ID" 

and

resSig <- cbind(transcript_ID=rownames(resSig), resSig)

But these did not result in the correct change.

CodePudding user response:

We may use rownames_to_column after converting to data.frame as the class is different

class(resSig)
[1] "DESeqResults"
attr(,"package")
[1] "DESeq2"

We convert to data.frame with as.data.frame and then do the rownames_to_column

library(dplyr)
library(tibble)
resSig <- resSig %>%
   as.data.frame %>%
   rownames_to_column("transcript_ID")

-output

resSig 
       transcript_ID   baseMean log2FoldChange     lfcSE     stat       pvalue         padj
1 ENSMUST00000103410.3 15237.9255       1.667017 0.4726054 3.527291 4.198356e-04 0.0097788597
2 ENSMUST00000194738.6   134.4450       1.909577 0.3913188 4.879849 1.061670e-06 0.0001551706
3 ENSMUST00000178282.3 24953.2773       1.523272 0.3601117 4.229998 2.336935e-05 0.0013347592
4 ENSMUST00000200568.2   675.4079       2.284544 0.5577937 4.095680 4.209318e-05 0.0020138101
5 ENSMUST00000196768.2    69.8793       2.997955 0.8722588 3.437002 5.881911e-04 0.0122055138
6 ENSMUST00000197515.2   119.6366       3.170698 0.7820734 4.054220 5.030189e-05 0.0022939429
> str(resSig)
data.frame':    6 obs. of  7 variables:
 $ transcript_ID : chr  "ENSMUST00000103410.3" "ENSMUST00000194738.6" "ENSMUST00000178282.3" "ENSMUST00000200568.2" ...
 $ baseMean      : num  15237.9 134.4 24953.3 675.4 69.9 ...
 $ log2FoldChange: num  1.67 1.91 1.52 2.28 3 ...
 $ lfcSE         : num  0.473 0.391 0.36 0.558 0.872 ...
 $ stat          : num  3.53 4.88 4.23 4.1 3.44 ...
 $ pvalue        : num  4.20e-04 1.06e-06 2.34e-05 4.21e-05 5.88e-04 ...
 $ padj          : num  0.009779 0.000155 0.001335 0.002014 0.012206 ...

NOTE: The OP's last piece of code also works after converting to data.frame

resSig <- as.data.frame(resSig)
resSig <- cbind(transcript_ID=rownames(resSig), resSig)
row.names(resSig) <- NULL
> resSig
         transcript_ID   baseMean log2FoldChange     lfcSE     stat       pvalue         padj
1 ENSMUST00000103410.3 15237.9255       1.667017 0.4726054 3.527291 4.198356e-04 0.0097788597
2 ENSMUST00000194738.6   134.4450       1.909577 0.3913188 4.879849 1.061670e-06 0.0001551706
3 ENSMUST00000178282.3 24953.2773       1.523272 0.3601117 4.229998 2.336935e-05 0.0013347592
4 ENSMUST00000200568.2   675.4079       2.284544 0.5577937 4.095680 4.209318e-05 0.0020138101
5 ENSMUST00000196768.2    69.8793       2.997955 0.8722588 3.437002 5.881911e-04 0.0122055138
6 ENSMUST00000197515.2   119.6366       3.170698 0.7820734 4.054220 5.030189e-05 0.0022939429

Writing the file gives

write.table(resSig, file = file.path(getwd(), "Downloads", 
    "ivan.txt"), row.names = FALSE, quote = FALSE)

-output

enter image description here

  • Related