Search code examples
rplyrcorrelationhmisc

Correlation matrix by group


This is my data frame

df <- structure(list(g1 = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", "C"), class = "factor"), g2 = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L), .Label = c("a", "b"), class = "factor"), v1 = 1:10, v2 = c(5, 5, 6, 2, 4, 4, 2, 1, 9, 8), v3 = c(29, 10, 56, 93, 20, 14, 12, 87, 67, 37)), .Names = c("g1", "g2",  "v1", "v2", "v3"), row.names = c(NA, -10L), class = "data.frame")

   g1 g2 v1 v2 v3
1   A  a  1  5 29
2   A  a  2  5 10
3   A  a  3  6 56
4   A  b  4  2 93
5   A  b  5  4 20
6   C  a  6  4 14
7   C  a  7  2 12
8   C  b  8  1 87
9   C  b  9  9 67
10  C  b 10  8 37

I'd like to create a correlation matrix of v1, v2 and v3 for each combination of groups g1 and g2 (Aa, Ab, Ca, Cb in this case). So I'd like to use package Hmisc and combine with plyr

library(Hmisc)
library(plyr)

This works (ignoring groups though of course):

rcorr(as.matrix(df[,3:5]), type="pearson")

But this does not:

cor.matrix <- dlply(df, .(g1,g2), rcorr(as.matrix(df[,3:5]), type="pearson"))
Error:attempt to apply non-function

What am I doing wrong?


Solution

  • This works if you have more than 4 observations per group (hence why I rbinded your df with an additional 2 more df):

    df <- structure(list(g1 = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), 
        .Label = c("A", "C"), class = "factor"), 
        g2 = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L), 
        .Label = c("a", "b"), class = "factor"), 
        v1 = 1:10, v2 = c(5, 5, 6, 2, 4, 4, 2, 1, 9, 8), 
        v3 = c(29, 10, 56, 93, 20, 14, 12, 87, 67, 37)), 
        .Names = c("g1", "g2",  "v1", "v2", "v3"), row.names = c(NA, -10L), 
        class = "data.frame")
    
    
    df <- rbind(df, df, df)
    
    library(Hmisc)
    lapply(split(df, df[, 1:2]), function(x) {
        rcorr(as.matrix(x[,3:5]), type="pearson")
    })
    

    EDIT This works:

    dlply(df, .(g1,g2), function(x) rcorr(as.matrix(x[,3:5]), type="pearson"))