# how to do a spearman correlation matrix for each of my independent variables?

I am asking for help to make a spearman correlation matrix where I can visualize how in each conditions if there are correlations between TAC (dependent variable) and concentration? If possible with `p.adjust` included.

The type of matrix I’m looking for is one that is easy to read, with Spearman’s p and p-value included. My thanks to anyone who can help me or point me in the right direction.

here is my data frame:

``````> str(table5)
'data.frame':   280 obs. of  5 variables:
\$ treatment    : chr  "control" "control" "control" "control" ...
\$ concentration: num  0 0 0 0 0 0 0 0 0 0 ...
\$ day          : chr  "day 00" "day 00" "day 00" "day 00" ...
\$ TAC          : num  0.0135 0.0162 0.0146 0.0153 0.0128 ...
\$ conditions   : Factor w/ 15 levels "controlday 00",..: 1 1 1 1 1 1 1 1 2 2 ...
> dput(table5)
structure(list(treatment = c("control", "control", "control",
"control", "control", "control", "control", "control", "control",
"control", "control", "control", "control", "control", "control",
"control", "control", "control", "control", "control", "control",
"control", "control", "control", "control", "control", "control",
"control", "control", "control", "control", "control", "control",
"control", "control", "control", "control", "control", "control",
"control", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn",
"nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn",
"nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn",
"nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn",
"nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn",
"nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn",
"nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn",
"nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn",
"nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn",
"nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn",
"nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn",
"nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn",
"nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn", "nZn",
"nZn", "nZn", "nZn", "nZn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn",
"Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn",
"Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn",
"Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn",
"Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn",
"Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn",
"Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn",
"Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn",
"Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn",
"Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn",
"Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn", "Zn",
"Zn", "Zn", "Zn", "Zn"), concentration = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100), day = c("day 00",
"day 00", "day 00", "day 00", "day 00", "day 00", "day 00", "day 00",
"day 07", "day 07", "day 07", "day 07", "day 07", "day 07", "day 07",
"day 07", "day 14", "day 14", "day 14", "day 14", "day 14", "day 14",
"day 14", "day 14", "day 21", "day 21", "day 21", "day 21", "day 21",
"day 21", "day 21", "day 21", "day 28", "day 28", "day 28", "day 28",
"day 28", "day 28", "day 28", "day 28", "day 00", "day 00", "day 00",
"day 00", "day 00", "day 00", "day 00", "day 00", "day 07", "day 07",
"day 07", "day 07", "day 07", "day 07", "day 07", "day 07", "day 14",
"day 14", "day 14", "day 14", "day 14", "day 14", "day 14", "day 14",
"day 21", "day 21", "day 21", "day 21", "day 21", "day 21", "day 21",
"day 21", "day 28", "day 28", "day 28", "day 28", "day 28", "day 28",
"day 28", "day 28", "day 00", "day 00", "day 00", "day 00", "day 00",
"day 00", "day 00", "day 00", "day 07", "day 07", "day 07", "day 07",
"day 07", "day 07", "day 07", "day 07", "day 14", "day 14", "day 14",
"day 14", "day 14", "day 14", "day 14", "day 14", "day 21", "day 21",
"day 21", "day 21", "day 21", "day 21", "day 21", "day 21", "day 28",
"day 28", "day 28", "day 28", "day 28", "day 28", "day 28", "day 28",
"day 00", "day 00", "day 00", "day 00", "day 00", "day 00", "day 00",
"day 00", "day 07", "day 07", "day 07", "day 07", "day 07", "day 07",
"day 07", "day 07", "day 14", "day 14", "day 14", "day 14", "day 14",
"day 14", "day 14", "day 14", "day 21", "day 21", "day 21", "day 21",
"day 21", "day 21", "day 21", "day 21", "day 28", "day 28", "day 28",
"day 28", "day 28", "day 28", "day 28", "day 28", "day 00", "day 00",
"day 00", "day 00", "day 00", "day 00", "day 00", "day 00", "day 07",
"day 07", "day 07", "day 07", "day 07", "day 07", "day 07", "day 07",
"day 14", "day 14", "day 14", "day 14", "day 14", "day 14", "day 14",
"day 14", "day 21", "day 21", "day 21", "day 21", "day 21", "day 21",
"day 21", "day 21", "day 28", "day 28", "day 28", "day 28", "day 28",
"day 28", "day 28", "day 28", "day 00", "day 00", "day 00", "day 00",
"day 00", "day 00", "day 00", "day 00", "day 07", "day 07", "day 07",
"day 07", "day 07", "day 07", "day 07", "day 07", "day 14", "day 14",
"day 14", "day 14", "day 14", "day 14", "day 14", "day 14", "day 21",
"day 21", "day 21", "day 21", "day 21", "day 21", "day 21", "day 21",
"day 28", "day 28", "day 28", "day 28", "day 28", "day 28", "day 28",
"day 28", "day 00", "day 00", "day 00", "day 00", "day 00", "day 00",
"day 00", "day 00", "day 07", "day 07", "day 07", "day 07", "day 07",
"day 07", "day 07", "day 07", "day 14", "day 14", "day 14", "day 14",
"day 14", "day 14", "day 14", "day 14", "day 21", "day 21", "day 21",
"day 21", "day 21", "day 21", "day 21", "day 21", "day 28", "day 28",
"day 28", "day 28", "day 28", "day 28", "day 28", "day 28"),
TAC = c(0.0134723395589115, 0.0161888871061509, 0.0146337654145718,
0.0153067871292595, 0.012800314735395, 0.0160841665978896,
0.0140621616691814, 0.0135425580967982, 0.0132198270328205,
0.0138496077219653, 0.0135775493518084, 0.0126333962864469,
0.0164821881641534, 0.0132516331108305, 0.0157791571175251,
0.0129960024291699, 0.0146323678504021, 0.0134451215151322,
0.0143262838325461, 0.0153573779185249, 0.0139773746147923,
0.0159350865128266, 0.0156720782857077, 0.0155096081292032,
0.013476349735956, 0.0140104181996115, 0.0129878390010014,
0.0147239859165112, 0.015160930718777, 0.0148955399340424,
0.013274378116328, 0.0153663044374496, 0.0145472559523844,
0.0132898660703847, 0.0139871399975842, 0.0124985111701027,
0.0149240276338179, 0.0129573902698069, 0.0147729343794709,
0.0128674264777598, 0.0147815872982594, 0.0139767796824041,
0.0144185398405766, 0.0155799146991459, 0.0135417909851351,
0.015988596586438, 0.0139603963976125, 0.0126397298299191,
0.013297964384596, 0.012347536157165, 0.0152573470818857,
0.0136566619097667, 0.0125192707022401, 0.0141156296691061,
0.0139603724286662, 0.0141388938152221, 0.0127749097766803,
0.0142082519110294, 0.0149398326676766, 0.0143207529313558,
0.0144381103787128, 0.0149147414885484, 0.0139224295866318,
0.0161358891403436, 0.0151690152511571, 0.0120945286936824,
0.0153132383654698, 0.0131770823852777, 0.0136750345235747,
0.0129352436377984, 0.0162120454010317, 0.0155409171425954,
0.0135940425474181, 0.0142951343511937, 0.0143779323175896,
0.0136891451722703, 0.0140286347004686, 0.0122667606250391,
0.0152446224172418, 0.013442306549535, 0.0129068996979612,
0.0147404146947943, 0.013688825582269, 0.0130193063055386,
0.01285971255513, 0.0151660181611206, 0.0138280467330508,
0.0135147736966651, 0.0158580706409006, 0.0149366602534351,
0.0106554950909403, 0.0179654260106192, 0.0120425346368713,
0.0145387164119486, 0.0139546280207597, 0.0121871897075845,
0.0150418870034593, 0.0148117380734173, 0.0139690179111281,
0.0170751257982307, 0.0129661477952429, 0.0144612227917873,
0.0146065893466387, 0.0126241343210384, 0.0170751257982307,
0.0130964557093226, 0.0134570968344701, 0.0165480203562944,
0.0151921149184481, 0.0130666062376204, 0.012722050697886,
0.0155582048904096, 0.0125288074742436, 0.016985639190516,
0.0176528351294189, 0.0138432089287227, 0.013890319218671,
0.017035215335001, 0.0168839977227436, 0.0133203267470888,
0.013892777179513, 0.0155216139064973, 0.0130076218759369,
0.013903958340264, 0.0135000204009635, 0.0148519977852621,
0.0153029154169557, 0.0141832966293512, 0.0176005510379328,
0.0180687740940438, 0.0177789446952697, 0.0182099087520794,
0.0184723827329167, 0.022483746075728, 0.0196648164641345,
0.0170131886149416, 0.0215058343136062, 0.0211259597744559,
0.0196373761289472, 0.0206737739206, 0.020532594441278, 0.0193494766153245,
0.0211617300063814, 0.0213333413267872, 0.0202163436360403,
0.0236752367085596, 0.0231873026647459, 0.0228522660496144,
0.0238366734630018, 0.0264524093818515, 0.0268093919646026,
0.0252668406573153, 0.0258403852690662, 0.0223986018317785,
0.0272147558779617, 0.0225116847733454, 0.0247724813762193,
0.022691182948792, 0.0235805783268122, 0.0270689051186104,
0.0126334908832258, 0.0164665820507107, 0.0129386884401034,
0.0119158011756844, 0.0130928729787235, 0.0149940706645974,
0.0129535502638655, 0.0162831996423606, 0.0176755444192191,
0.0161755659998132, 0.0174173101524856, 0.0155714069341957,
0.01433383826834, 0.0143819293817603, 0.0185494616259894,
0.0140319779691521, 0.0144114680062016, 0.0174497227904159,
0.0180907703704672, 0.0157478259355293, 0.0158958906812569,
0.0147163839619763, 0.0146701443994308, 0.0180369287296324,
0.0149336258279806, 0.0186097801562105, 0.0137231521985133,
0.0153650910635747, 0.0138998273293687, 0.0155199902217533,
0.0163903022171882, 0.015754928008943, 0.0171808546793322,
0.0154244829039175, 0.0134954450270778, 0.0147187179502944,
0.0160939056001929, 0.0145497150558122, 0.0154571534643691,
0.015511148172344, 0.0132885919777709, 0.0138910418368534,
0.0152496449072613, 0.0132820365830201, 0.013480084079182,
0.016683045565325, 0.0176337406920335, 0.0151657804062655,
0.0125455114843902, 0.0118102856445592, 0.0116410665300014,
0.0146556231989517, 0.014464999427952, 0.0121229802720933,
0.0146834533301593, 0.0121645122630423, 0.0136816673389857,
0.0135984961089614, 0.0164906141382343, 0.0149265724276527,
0.0163311308492402, 0.017967595623527, 0.0143263172313383,
0.0145117513172078, 0.0149694356038913, 0.0136478358101476,
0.0148523043836901, 0.0140267859486034, 0.0136857372651645,
0.0161384954212, 0.0171836598216303, 0.0165288287203719,
0.0163703032374203, 0.0149628937118673, 0.0167639896711626,
0.0144140290861155, 0.0164700832677882, 0.017097353142466,
0.0177233791174971, 0.016410406871025, 0.0145656397252108,
0.0127795571441824, 0.0139787766512734, 0.0145603577832239,
0.0130325210010334, 0.0157142193796273, 0.0165295708322065,
0.0154878492755022, 0.0176888974165639, 0.0186435561581489,
0.0177330425080685, 0.0182856446463086, 0.0219973970170363,
0.0217533371623466, 0.0176290655250839, 0.0202192044566584,
0.01917805317661, 0.0186277616395779, 0.0170154664932417,
0.0195884686724334, 0.0201420675026667, 0.0183148068985733,
0.020836323932372, 0.0207067552945439, 0.018534989031893,
0.019680916901509, 0.0219673944081694, 0.0236890701508884,
0.0235543150426157, 0.0234233849979097, 0.0210565415662947,
0.0232511101944444, 0.0227186732866978, 0.0225332903957415,
0.0234773944195847, 0.0229988542468931, 0.022618525386521,
0.0197686090869307, 0.0186686467858637, 0.0189525178016395
), conditions = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L), levels = c("controlday 00",
"controlday 07", "controlday 14", "controlday 21", "controlday 28",
"nZnday 00", "nZnday 07", "nZnday 14", "nZnday 21", "nZnday 28",
"Znday 00", "Znday 07", "Znday 14", "Znday 21", "Znday 28"
), class = "factor")), class = "data.frame", row.names = c(NA,
-280L))
``````

### >Solution :

You could `split` the data frame by `conditions`, use `lapply` to get the `cor.test` from each sub-data frame and create a one-row data frame of group/correlation/pvalue from each, which you then `rbind` into a single data frame.

``````do.call(rbind, lapply(split(table5, table5\$conditions), function(d) {
x <- cor.test(d\$concentration, d\$TAC, method = 'spearman')
data.frame(group = d\$conditions[1], cor = x\$estimate,
p = scales::pvalue(x\$p.value, add_p = TRUE))
})) |> `rownames<-`(NULL)
#>            group         cor       p
#> 1  controlday 00          NA    <NA>
#> 2  controlday 07          NA    <NA>
#> 3  controlday 14          NA    <NA>
#> 4  controlday 21          NA    <NA>
#> 5  controlday 28          NA    <NA>
#> 6      nZnday 00 -0.05160468 p=0.811
#> 7      nZnday 07  0.70034929 p<0.001
#> 8      nZnday 14  0.71509349 p<0.001
#> 9      nZnday 21  0.73720978 p<0.001
#> 10     nZnday 28  0.78144237 p<0.001
#> 11      Znday 00  0.23590713 p=0.267
#> 12      Znday 07  0.46444216 p=0.022
#> 13      Znday 14  0.56765153 p=0.004
#> 14      Znday 21  0.65611670 p<0.001
#> 15      Znday 28  0.81830286 p<0.001
``````

The first four groups all have NA values, because the value of `concentration` is 0 for the entire group and the standard deviation is therefore 0.