<p>You can use the function <code>corAndPvalue</code>. Simply replace the lines</p>

<pre><code>moduleTraitCor= cor(MEs, status, use="p")
moduleTraitPvalue = corPvalueStudent(moduleTraitCor, nSamples)
</code></pre>

<p>by something like</p>

<pre><code>cp = corAndPvalue(MEs, status)
moduleTraitCor = cp<span class="math-container">$cor
moduleTraitPvalue = cp$</span>p
</code></pre>

<p><code>corAndPvalue</code> takes automatically into account the number of valid samples for each element of the resulting correlation matrix.</p>

CC BY-SA 4.0
2019-11-21T13:21:32.117Z
Thank you so much, as always!
Ruth Johnson
CC BY-SA 4.0
2019-11-20T20:52:51.853Z
2019-11-20T20:52:51.853Z
Peter Langfelder
<p>I am correcting a data frame of sample comparison using p value. However in some columns I have different sample numbers. Within the data frame I have 4 comparisons e.g B_R, R_C, B_C, C_C. They each have different sample numbers in the comparisons. I need to create a loop or change the code so I can vary the nSamples numbers for each of the comparisons. I have tried to do this for each comparison separately but to no avail. Does anyone have any suggestions? Thank you. I have tried.. </p>

<pre><code>nGenes = ncol(cells)
nSamples = nrow(cells)

MEs0 = moduleEigengenes(cells, moduleColors)$eigengenes
MEs = orderMEs(MEs0)
moduleTraitCor= cor(MEs, status, use="p")
moduleTraitPvalue = corPvalueStudent(moduleTraitCor, nSamples)

sizeGrWindow(10,6)
textMatrix = paste(signif(moduleTraitCor, 2), "\n(",
                   signif(moduleTraitPvalue, 1), ")", sep = "")  ``````````

I have tried to code for the varying Sample numbers using:

MEs0 = moduleEigengenes(cell, moduleColors)<span class="math-container">$eigengenes
MEs = orderMEs(MEs0)
moduleTraitCor= cor(MEs, NR_status, use="p")
moduleTraitPvalue$</span>B_R = corPvalueStudent(moduleTraitCor, 32)
moduleTraitPvalue<span class="math-container">$R_C = corPvalueStudent(moduleTraitCor, 31)
moduleTraitPvalue$</span>B_C = corPvalueStudent(moduleTraitCor, 47)
moduleTraitPvalue$C_C = corPvalueStudent(moduleTraitCor, 31)

sizeGrWindow(10,6)
textMatrix = paste(signif(moduleTraitCor, 2), "\n(",
                   signif(moduleTraitPvalue<span class="math-container">$NR_R,
                      moduleTraitPvalue$</span>R_C,
                          moduleTraitPvalue<span class="math-container">$C_C,
                      moduleTraitPvalue$</span>NR_C, 1), ")", sep = "")
</code></pre>

<r><wgcna>
WGCNA correcting p value for varying sample numbers in each of the trait columns
# WGCNA correcting p value for varying sample numbers in each of the trait columns

(Asked by: username_0 on 2019-11-20)

I am correcting a data frame of sample comparison using p value. However in some columns I have different sample numbers. Within the data frame I have 4 comparisons e.g B_R, R_C, B_C, C_C. They each have different sample numbers in the comparisons. I need to create a loop or change the code so I can vary the nSamples numbers for each of the comparisons. I have tried to do this for each comparison separately but to no avail. Does anyone have any suggestions? Thank you. I have tried.. 
nGenes = ncol(cells)
nSamples = nrow(cells)

MEs0 = moduleEigengenes(cells, moduleColors)$eigengenes
MEs = orderMEs(MEs0)
moduleTraitCor= cor(MEs, status, use="p")
moduleTraitPvalue = corPvalueStudent(moduleTraitCor, nSamples)

sizeGrWindow(10,6)
textMatrix = paste(signif(moduleTraitCor, 2), "\n(",
                   signif(moduleTraitPvalue, 1), ")", sep = "")  ``````````

I have tried to code for the varying Sample numbers using:

MEs0 = moduleEigengenes(cell, moduleColors)$eigengenes
MEs = orderMEs(MEs0)
moduleTraitCor= cor(MEs, NR_status, use="p")
moduleTraitPvalue$B_R = corPvalueStudent(moduleTraitCor, 32)
moduleTraitPvalue$R_C = corPvalueStudent(moduleTraitCor, 31)
moduleTraitPvalue$B_C = corPvalueStudent(moduleTraitCor, 47)
moduleTraitPvalue$C_C = corPvalueStudent(moduleTraitCor, 31)

sizeGrWindow(10,6)
textMatrix = paste(signif(moduleTraitCor, 2), "\n(",
                   signif(moduleTraitPvalue$NR_R,
                      moduleTraitPvalue$R_C,
                          moduleTraitPvalue$C_C,
                      moduleTraitPvalue$NR_C, 1), ")", sep = "")

**Tags:** r, wgcna

---

## 1 answers

### Answer by username_1 on 2019-11-20. Score: 0 | Accepted answer

You can use the function corAndPvalue. Simply replace the lines
moduleTraitCor= cor(MEs, status, use="p")
moduleTraitPvalue = corPvalueStudent(moduleTraitCor, nSamples)

by something like
cp = corAndPvalue(MEs, status)
moduleTraitCor = cp$cor
moduleTraitPvalue = cp$p

corAndPvalue takes automatically into account the number of valid samples for each element of the resulting correlation matrix.

> **Comments:**
> **username_0**: Thank you so much, as always!  

---

