rcorrelationsignificance

Column by column correlation & significance between two data sets


I would like to use the corr.test function from the psych package in order to calculate the correlation and the significance between corresponding columns of two dataframes. A simplified example of the dataframes Df1 and Df2 I am working with is this:

set.seed(42)
Df1 <- data.frame(matrix(runif(50), 10, 5))
Df2 <- data.frame(matrix(runif(50), 10, 5))

Please note that this question has been already answered here:

Column by column correlation between two data sets with R?

but only for the the correlation part, i.e., it lacks the significance I am looking for, since it uses the cor function and not the corr.test one.

Any help would be greatly appreciated.


Solution

  • Using cor.test in mapply and subsetting desired statistics, where estimate is the correlation.

    mapply(\(x, y) cor.test(x, y)[c('estimate', 'p.value')], Df1, Df2)
    #                 X1        X2        X3         X4          X5       
    # estimate 0.2486405 -0.408098 0.03718413 -0.09967868 0.4662738
    # p.value  0.4884952 0.2416943 0.9187721  0.7841065   0.1743502