3

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?

erc
  • 10,113
  • 11
  • 57
  • 88
  • 3
    How 'bout this? `by(df, INDICES = list(df$g1, df$g2), FUN = function(x) cor(x[, c("v1", "v2", "v3")]))` – Roman Luštrik May 31 '14 at 07:12
  • That works great, thanks, however, the reason why I wanted to use `rcorr` from Hmisc is that it also generates a matrix with p-values. I don't think this is possible with `cor`? – erc May 31 '14 at 07:49

1 Answers1

2

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"))
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • Perfect, thanks! Still, do you have an explanation why `dlply` does not work? Just curious ;) – erc May 31 '14 at 19:41