Search code examples
rdplyrcorrelationp-valuesummarize

getting p-values for a pairwise correlation (dplyr)


I am using the code below to get correlations between my dependent variable and a questionnaire response (for different levels of different conditions).

BREAK %>%
    group_by(condition, valence) %>%
    summarize(COR=cor(rt, positive_focused_cognitiveER)) %>%
    ungroup()

It gives me the correlations and their directions (+/-). I would like to know, however, if those correlations are significant. Is there a way to simply add a line to the code I already have to get the p-values? Or another easy code? (I don't need fancy stuff, just the numbers)

The only fitting post I found for my problem was this one Getting p values for groupwise correlation using the dplyr package but the answer did not help me.

Thanks in advance for any tips! :)


Solution

  • You can compute p-values with stats::cor.test :

    BREAK %>%
            group_by(condition, valence) %>%
            summarize(COR = stats::cor.test(rt, positive_focused_cognitiveER)$estimate,
                      pval = stats::cor.test(rt, positive_focused_cognitiveER)$p.value
                      ) %>%
            ungroup()