How to add new column name to empty header in a list elements
1
0
Entering edit mode
kcm • 0
@2cdf3f50
Last seen 2.9 years ago
India

Objective how to assign a col-name to an empty header inside a list

My data structure Code should be placed in three backticks as shown below

results1 <- lapply(contrasts, function(x) results(dds, contrast=x)) ## my code 
## Sample output 
results1
$M0_vs_M1
log2 fold change (MLE): FAB M0 vs M1 
Wald test p-value: FAB M0 vs M1 
DataFrame with 51380 rows and 6 columns
                          baseMean      log2FoldChange             lfcSE                stat              pvalue               padj
                         <numeric>           <numeric>         <numeric>           <numeric>           <numeric>          <numeric>
ENSG00000223972   1.35719895705297   0.167510490419353  0.46667402535746   0.358945390824022   0.719635942258462  0.856597394003598
ENSG00000227232   76.8560719439685  -0.705608168485163 0.219196495449407   -3.21906683333824 0.00128608506388972 0.0158343815011299
ENSG00000278267   4.90974256671574   -1.16851163038749 0.436136368072954   -2.67923456039794 0.00737906827756855 0.0508245749793705
ENSG00000243485  0.295581734786796    0.16890551310833 0.980082656456368   0.172338028834254    0.86317178889973                 NA
ENSG00000237613 0.0612332522952744   0.265097343084784  3.52887790189744  0.0751222769544518   0.940117423456592                 NA
...                            ...                 ...               ...                 ...                 ...                ...
ENSG00000215506  0.531980504702335  -0.554637704219196 0.843139724240875  -0.657824187703369   0.510651110092843                 NA
ENSG00000224240 0.0555883982498256 -0.0875512872565122  3.56201998641903 -0.0245791117372503   0.980390680685091                 NA
ENSG00000227629 0.0765793340097447   0.297144162185063  2.90839061586321   0.102167900200321   0.918623406325407                 NA
ENSG00000237917   14.1547350663573 -0.0621818678501326 0.505199963776219   -0.12308367440358    0.90204083891314  0.956184083343483
ENSG00000231514  0.164301605186092    -0.5684666000495  1.47572077171201  -0.385212847136394   0.700079725794659                 NA

$M0_vs_M2
log2 fold change (MLE): FAB M0 vs M2 
Wald test p-value: FAB M0 vs M2 
DataFrame with 51380 rows and 6 columns
                          baseMean      log2FoldChange             lfcSE                stat              pvalue               padj
                         <numeric>           <numeric>         <numeric>           <numeric>           <numeric>          <numeric>
ENSG00000223972   1.35719895705297   0.546991630317625 0.476229496776925    1.14858830462962   0.250725781858858  0.507965684166553
ENSG00000227232   76.8560719439685   -0.62569393681632 0.218440258775195   -2.86437097412637 0.00417838341460869 0.0415066059471346
ENSG00000278267   4.90974256671574 -0.0129648769610915 0.445103345323286 -0.0291277904273554   0.976762671641514   0.99028984958177
ENSG00000243485  0.295581734786796  -0.846240912635684 0.969758036480007  -0.872630987114414   0.382864244993462                 NA
ENSG00000237613 0.0612332522952744   0.258612647154544  3.51482364475393  0.0735777021246965   0.941346414163943                 NA

# include your problematic code here with any corresponding output 
# please also include the results of running the following in an R session 

In the above output this is the format for all the elements in the list

DataFrame with 51380 rows and 6 columns

    baseMean      log2FoldChange             lfcSE                stat              pvalue               padj

My objective is to add a col name to the empty header the column which is before the baseMean .

I would like to save the output to do this Im running this

for(i in 1:length(results1)){
  results = as.data.frame(results1[[i]]) 
  write.table(results,file=paste0(names(results1)[i],"_TCGA_stages.txt"),sep = "\t",quote = FALSE,row.names = TRUE)
}

I get output like this

baseMean log2FoldChange     lfcSE      stat       pvalue         padj UP_DOWN
ENSG00000237973 10550.545143      -1.626252 0.3987092 -4.078792 4.527033e-05 0.0015844000    DOWN
ENSG00000234396     1.313836       1.513997 0.5069778  2.986318 2.823587e-03 0.0267502859      UP
ENSG00000078900   159.209635      -1.708001 0.4537402 -3.764271 1.670355e-04 0.0040623446    DOWN
ENSG00000235131     3.292931      -2.277330 0.6918335 -3.291732 9.957256e-04 0.0134204677    DOWN
ENSG00000196581    10.582909       1.822511 0.6121410  2.977273 2.908251e-03 0.0273166596      UP
ENSG00000187017    79.130853      -2.709708 0.5869882 -4.616290 3.906606e-06 0.0002934384     Low

Here the issue is the column containing the ENSG id is now empty, which I want to assign name as gene I can rename each file but again its a long process to rename each file individually. So how to do that before i save my list into individual or in other words after this step

results1 <- lapply(contrasts, function(x) results(dds, contrast=x)) ## my code 

My final expected output

gene     baseMean log2FoldChange     lfcSE      stat       pvalue         padj UP_DOWN
1 ENSG00000237973 10550.545143      -1.626252 0.3987092 -4.078792 4.527033e-05 0.0015844000    DOWN
2 ENSG00000234396     1.313836       1.513997 0.5069778  2.986318 2.823587e-03 0.0267502859      UP
3 ENSG00000078900   159.209635      -1.708001 0.4537402 -3.764271 1.670355e-04 0.0040623446    DOWN
4 ENSG00000235131     3.292931      -2.277330 0.6918335 -3.291732 9.957256e-04 0.0134204677    DOWN
5 ENSG00000196581    10.582909       1.822511 0.6121410  2.977273 2.908251e-03 0.0273166596      UP
6 ENSG00000187017    79.130853      -2.709708 0.5869882 -4.616290 3.906606e-06 0.0002934384     Low

Any suggestion or help would be really appreciated .

DESeq2 • 782 views
ADD COMMENT
0
Entering edit mode
@mikelove
Last seen 1 day ago
United States

This isn't really a DESeq2 question, but it sounds like you want the rowname to be a column. There are dedicated functions for this, e.g. rownames_to_column in tibble package, or Googling "rownames to column" one of the top results:

https://stackoverflow.com/a/43125122

ADD COMMENT

Login before adding your answer.

Traffic: 953 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6