Correlations for selected variables of df and for loop(s)

H All,
I would like to calculate correlations between variables: P18, P19, P20, P21, P22, P23 - each variable in turn with variables: P35, P36, P37, P38, P39, P40, P41.
So first will be correlations between variable P18 and P35, P36, P37, P38, P39, P40, P41 variables and then P19 and P35, P36, P37, P38, P39, P40, P41 variables and so on.
I would like to store the results in dataframe with "correlation result" and "p-value" as columns.
How do I do it with for loop, please ? Any ideas will be greatly appreciated.

I have done it sort of manually one by one steps, but I want to do it using for loop in a more automatic way.
I have done this using manual calculations and with rbind() function later on.
I would like to do it for all rows in data frame (as a whole) and for "Location" levels subgroups ("Group1", "Group2", "Group3", "Group4") separately.
How do I do it please.

Here below there are my desired results as an example:

Here below there is my data frame:

dfM <- structure(list(Location = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L), .Label = c("Group1", "Group2", "Group3", "Group4"), class = "factor"), 
    P18 = c(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, 0, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1), P19 = c(1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 
    0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 
    1, 0, 1, 0, 0, 0, 0, 0, 0), P20 = c(0, 0, 0, 1, 0, 1, 0, 
    0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 
    0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1), P21 = c(0, 0, 
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 
    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    P22 = c(0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 
    0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 
    0, 0, 0, 0), P23 = 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), P35 = c(1, 1, 0, 0, 1, 0, 1, 
    0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 
    1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1), P36 = c(0, 0, 
    0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 
    1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0), 
    P37 = c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), P38 = 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), P39 = c(0, 0, 0, 1, 0, 1, 1, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0), P40 = 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), 
    P41 = 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)), class = "data.frame", row.names = c(NA, -40L
))

thank you and kind regards.

library(corrplot)
#> corrplot 0.92 loaded
dfM <- structure(list(
  Location = structure(c(
    1L, 1L, 1L, 1L, 1L, 1L,
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
    4L, 4L
  ), .Label = c("Group1", "Group2", "Group3", "Group4"), class = "factor"),
  P18 = c(
    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, 0, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1
  ), P19 = c(
    1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0,
    0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0,
    1, 0, 1, 0, 0, 0, 0, 0, 0
  ), P20 = c(
    0, 0, 0, 1, 0, 1, 0,
    0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1,
    0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1
  ), P21 = c(
    0, 0,
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  ),
  P22 = c(
    0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
    0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0,
    0, 0, 0, 0
  ), P23 = 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
  ), P35 = c(
    1, 1, 0, 0, 1, 0, 1,
    0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0,
    1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1
  ), P36 = c(
    0, 0,
    0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0,
    1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0
  ),
  P37 = c(
    0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0
  ), P38 = 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
  ), P39 = c(
    0, 0, 0, 1, 0, 1, 1,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0
  ), P40 = 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
  ),
  P41 = 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
  )
), class = "data.frame", row.names = c(NA, -40L))

M <- cor(dfM[,2:14])
#> Warning in cor(dfM[, 2:14]): the standard deviation is zero
M
#>             P18         P19         P20         P21         P22 P23         P35
#> P18  1.00000000 -0.17702833  0.13766649 -0.56235159  0.11750194  NA  0.17702833
#> P19 -0.17702833  1.00000000  0.13723276  0.31480009  0.28446279  NA  0.01010101
#> P20  0.13766649  0.13723276  1.00000000 -0.05280108  0.11132949  NA -0.13723276
#> P21 -0.56235159  0.31480009 -0.05280108  1.00000000  0.18904771  NA -0.31480009
#> P22  0.11750194  0.28446279  0.11132949  0.18904771  1.00000000  NA -0.07374961
#> P23          NA          NA          NA          NA          NA   1          NA
#> P35  0.17702833  0.01010101 -0.13723276 -0.31480009 -0.07374961  NA  1.00000000
#> P36 -0.20672456 -0.07784989  0.16974983  0.17155008  0.08119979  NA -0.75254898
#> P37  0.03673592 -0.20751434 -0.19723489 -0.06532553 -0.16834512  NA  0.20751434
#> P38          NA          NA          NA          NA          NA  NA          NA
#> P39  0.06726728 -0.09851341  0.06373369 -0.11961783 -0.16146816  NA -0.18295348
#> P40          NA          NA          NA          NA          NA  NA          NA
#> P41          NA          NA          NA          NA          NA  NA          NA
#>             P36         P37 P38         P39 P40 P41
#> P18 -0.20672456  0.03673592  NA  0.06726728  NA  NA
#> P19 -0.07784989 -0.20751434  NA -0.09851341  NA  NA
#> P20  0.16974983 -0.19723489  NA  0.06373369  NA  NA
#> P21  0.17155008 -0.06532553  NA -0.11961783  NA  NA
#> P22  0.08119979 -0.16834512  NA -0.16146816  NA  NA
#> P23          NA          NA  NA          NA  NA  NA
#> P35 -0.75254898  0.20751434  NA -0.18295348  NA  NA
#> P36  1.00000000 -0.17770466  NA -0.18077538  NA  NA
#> P37 -0.17770466  1.00000000  NA  0.22487240  NA  NA
#> P38          NA          NA   1          NA  NA  NA
#> P39 -0.18077538  0.22487240  NA  1.00000000  NA  NA
#> P40          NA          NA  NA          NA   1  NA
#> P41          NA          NA  NA          NA  NA   1
corrplot(M,method = "number", col = "black")

# groups

g1 <- dfM[which(dfM$Location == "Group1"),]

M1 <- cor(g1[,2:14])
#> Warning in cor(g1[, 2:14]): the standard deviation is zero
M1
#>     P18        P19        P20        P21        P22 P23        P35        P36
#> P18   1         NA         NA         NA         NA  NA         NA         NA
#> P19  NA  1.0000000 -0.1666667  0.2721655  0.2500000  NA  0.1666667 -0.6123724
#> P20  NA -0.1666667  1.0000000 -0.2721655 -0.2500000  NA -0.1666667  0.1020621
#> P21  NA  0.2721655 -0.2721655  1.0000000  0.4082483  NA -0.4082483 -0.1666667
#> P22  NA  0.2500000 -0.2500000  0.4082483  1.0000000  NA  0.2500000 -0.4082483
#> P23  NA         NA         NA         NA         NA   1         NA         NA
#> P35  NA  0.1666667 -0.1666667 -0.4082483  0.2500000  NA  1.0000000 -0.6123724
#> P36  NA -0.6123724  0.1020621 -0.1666667 -0.4082483  NA -0.6123724  1.0000000
#> P37  NA -0.4082483 -0.2721655 -0.1111111 -0.2721655  NA  0.2721655 -0.1666667
#> P38  NA         NA         NA         NA         NA  NA         NA         NA
#> P39  NA -0.3563483  0.3563483 -0.2182179 -0.5345225  NA -0.3563483  0.2182179
#> P40  NA         NA         NA         NA         NA  NA         NA         NA
#> P41  NA         NA         NA         NA         NA  NA         NA         NA
#>            P37 P38        P39 P40 P41
#> P18         NA  NA         NA  NA  NA
#> P19 -0.4082483  NA -0.3563483  NA  NA
#> P20 -0.2721655  NA  0.3563483  NA  NA
#> P21 -0.1111111  NA -0.2182179  NA  NA
#> P22 -0.2721655  NA -0.5345225  NA  NA
#> P23         NA  NA         NA  NA  NA
#> P35  0.2721655  NA -0.3563483  NA  NA
#> P36 -0.1666667  NA  0.2182179  NA  NA
#> P37  1.0000000  NA  0.5091751  NA  NA
#> P38         NA   1         NA  NA  NA
#> P39  0.5091751  NA  1.0000000  NA  NA
#> P40         NA  NA         NA   1  NA
#> P41         NA  NA         NA  NA   1
corrplot(M1,method = "number", col = "black")

Thank you very much indeed @technocrat for taking time and effort in helping me.

This is a correlation matrix you provided, I would like to have results as a dataframe more like this if possible:

and with selected variables only.

Convert to data frame and then use {dplyr}select for columns and {dplyr}slice for rows or DF[row,col] with subset operator.

library(corrplot)
#> corrplot 0.92 loaded
dfM <- structure(list(
  Location = structure(c(
    1L, 1L, 1L, 1L, 1L, 1L,
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
    4L, 4L
  ), .Label = c("Group1", "Group2", "Group3", "Group4"), class = "factor"),
  P18 = c(
    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, 0, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1
  ), P19 = c(
    1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0,
    0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0,
    1, 0, 1, 0, 0, 0, 0, 0, 0
  ), P20 = c(
    0, 0, 0, 1, 0, 1, 0,
    0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1,
    0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1
  ), P21 = c(
    0, 0,
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  ),
  P22 = c(
    0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
    0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0,
    0, 0, 0, 0
  ), P23 = 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
  ), P35 = c(
    1, 1, 0, 0, 1, 0, 1,
    0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0,
    1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1
  ), P36 = c(
    0, 0,
    0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0,
    1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0
  ),
  P37 = c(
    0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0
  ), P38 = 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
  ), P39 = c(
    0, 0, 0, 1, 0, 1, 1,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0
  ), P40 = 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
  ),
  P41 = 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
  )
), class = "data.frame", row.names = c(NA, -40L))

M <- cor(dfM[,2:14])
#> Warning in cor(dfM[, 2:14]): the standard deviation is zero
as.data.frame(M)
#>             P18         P19         P20         P21         P22 P23         P35
#> P18  1.00000000 -0.17702833  0.13766649 -0.56235159  0.11750194  NA  0.17702833
#> P19 -0.17702833  1.00000000  0.13723276  0.31480009  0.28446279  NA  0.01010101
#> P20  0.13766649  0.13723276  1.00000000 -0.05280108  0.11132949  NA -0.13723276
#> P21 -0.56235159  0.31480009 -0.05280108  1.00000000  0.18904771  NA -0.31480009
#> P22  0.11750194  0.28446279  0.11132949  0.18904771  1.00000000  NA -0.07374961
#> P23          NA          NA          NA          NA          NA   1          NA
#> P35  0.17702833  0.01010101 -0.13723276 -0.31480009 -0.07374961  NA  1.00000000
#> P36 -0.20672456 -0.07784989  0.16974983  0.17155008  0.08119979  NA -0.75254898
#> P37  0.03673592 -0.20751434 -0.19723489 -0.06532553 -0.16834512  NA  0.20751434
#> P38          NA          NA          NA          NA          NA  NA          NA
#> P39  0.06726728 -0.09851341  0.06373369 -0.11961783 -0.16146816  NA -0.18295348
#> P40          NA          NA          NA          NA          NA  NA          NA
#> P41          NA          NA          NA          NA          NA  NA          NA
#>             P36         P37 P38         P39 P40 P41
#> P18 -0.20672456  0.03673592  NA  0.06726728  NA  NA
#> P19 -0.07784989 -0.20751434  NA -0.09851341  NA  NA
#> P20  0.16974983 -0.19723489  NA  0.06373369  NA  NA
#> P21  0.17155008 -0.06532553  NA -0.11961783  NA  NA
#> P22  0.08119979 -0.16834512  NA -0.16146816  NA  NA
#> P23          NA          NA  NA          NA  NA  NA
#> P35 -0.75254898  0.20751434  NA -0.18295348  NA  NA
#> P36  1.00000000 -0.17770466  NA -0.18077538  NA  NA
#> P37 -0.17770466  1.00000000  NA  0.22487240  NA  NA
#> P38          NA          NA   1          NA  NA  NA
#> P39 -0.18077538  0.22487240  NA  1.00000000  NA  NA
#> P40          NA          NA  NA          NA   1  NA
#> P41          NA          NA  NA          NA  NA   1

Here is another approach using the purrr package's map() functions.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
dfM <- structure(list(Location = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
                                             1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
                                             3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                             4L, 4L), .Label = c("Group1", "Group2", "Group3", "Group4"), class = "factor"), 
                      P18 = c(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, 0, 1, 1, 1, 1, 1, 1, 1, 
                              1, 1, 1, 1), P19 = c(1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 
                                                   0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 
                                                   1, 0, 1, 0, 0, 0, 0, 0, 0), P20 = c(0, 0, 0, 1, 0, 1, 0, 
                                                                                       0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 
                                                                                       0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1), P21 = c(0, 0, 
                                                                                                                                          1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 
                                                                                                                                          0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
                      P22 = c(0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 
                              0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 
                              0, 0, 0, 0), P23 = 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), P35 = c(1, 1, 0, 0, 1, 0, 1, 
                                                                                       0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 
                                                                                       1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1), P36 = c(0, 0, 
                                                                                                                                          0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 
                                                                                                                                          1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0), 
                      P37 = c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 
                              0, 0, 0, 0), P38 = 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), P39 = c(0, 0, 0, 1, 0, 1, 1, 
                                                                                       0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 
                                                                                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0), P40 = 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), 
                      P41 = 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)), class = "data.frame", row.names = c(NA, -40L
                              ))

Targ <- dfM |> select(P35:P41)
Orig <-  dfM |> select(P18:P23)
DFrow <- function(Org){
  map_dfc(Targ,.f=~cor(.x,Org))
}
CorDF <- map_dfr(Orig, DFrow)
#> Warning in cor(.x, Org): the standard deviation is zero
#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero

#> Warning in cor(.x, Org): the standard deviation is zero
CorDF$Var <- colnames(Orig)
CorDF
#> # A tibble: 6 x 8
#>       P35     P36     P37   P38     P39   P40   P41 Var  
#>     <dbl>   <dbl>   <dbl> <dbl>   <dbl> <dbl> <dbl> <chr>
#> 1  0.177  -0.207   0.0367    NA  0.0673    NA    NA P18  
#> 2  0.0101 -0.0778 -0.208     NA -0.0985    NA    NA P19  
#> 3 -0.137   0.170  -0.197     NA  0.0637    NA    NA P20  
#> 4 -0.315   0.172  -0.0653    NA -0.120     NA    NA P21  
#> 5 -0.0737  0.0812 -0.168     NA -0.161     NA    NA P22  
#> 6 NA      NA      NA         NA NA         NA    NA P23

Created on 2022-02-06 by the reprex package (v2.0.1)

Thank you very much @FJCC,
Your solution gives a correlation results that I want, this is very good. Can you advise how to get p-values out of cor() function as well, please ?

The cor.test() function returns a p value. You can get that with

cor.test(x, y)$p.value

When I have changed your code:

DFrow <- function(Org){
  map_dfc(Targ,.f=~cor.test(.x,Org))
}

CorDF <- map_dfr(Orig, DFrow)
Error in `stop_vctrs()`:
! Input must be a vector, not a <htest> object.

cor.test returns a list. To get just the p values, use

DFrow <- function(Org){
  map_dfc(Targ,.f=~cor.test(.x,Org)$p.value)
}

Thank you very much, now it works.

Hi Andrzej,

Applyng this function directly to your data you can obtain the two matrix stored in a list.

get_cor_pvalue <- function(df){
  df <- df[,sapply(df, is.numeric)]
  df_cor <- data.frame()
  df_pvalues <- data.frame()
  for (i in 1:ncol(df)) {
    x <- df[[i]]
    for (j in 1:ncol(df)){
      y <- df[[j]]
      cor_xy <- cor.test(x,y)
      df_cor[i,j] <- cor_xy$estimate
      df_pvalues[i,j] <- cor_xy$p.value
    }
  }
  colnames(df_cor) <- colnames(df)
  rownames(df_cor) <- colnames(df)
  colnames(df_pvalues) <- colnames(df)
  rownames(df_pvalues) <- colnames(df)
  return(list('cor'=df_cor,'p_values'=df_pvalues))
}
results <- get_cor_pvalue(dfM)
# to access matrix:
results$cor # matrix of correlations
results$pvalues # matrix with pvalues

This is the output:

Thank you very much @Adan1, this is great solution. It should be possibility to mark here as second solution. Thanks again.

Hi @Adan1, or anybody

Would you care about elaborate a bit what is going on in your function and especially in for loops, please ?
I would like to better understand your code myself. What do i and j represent here ?

Hi @Andrzej ,

If you are not familiar with loops, I think the first part of this book can be very helpful.

In the first loop, 'i' takes the values from 1 to the number of columns of your dataframe (1,2,3,4,5...), so in the first iteration, since 'x'=1 the first variable ( df[[i]] ) is assing to vector 'x'. Also 'j' takes the same values as 'i' and we assign to vector 'y'.

So in the first iteration, x and y are the first columns of your data (i=1, j=1 so corr = 1), in the second 'x' is the first column (and i=1) and 'y' the second (j=2), and so on. Notice that before 'i' changes its value, 'j' had all the possible values. Once 'j' is your last column, in the following iteration 'x' will be your second column since 'i' is now equal 2, and we start again to calcul all the correlations. Thus, you obtain the correlations (cor.test) and store it in 'cor_xy', and then assign the values to the two dataframes (df_cor for correlations, and df_pvalue for pvalues).

This is the logic of the nested-loop:

i = 1
j = 1,2,3,4,5,6...

i = 2
j = 1,2,3,4,5,6...

i = 3
j=1,2,3,4,5,6...

...

Hope this will help you,

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.