I have a data frame with measurements made by different raters, and I want to calculate the correlation of measurements between raters.
Here's my current implementation with dummy data:
set.seed(123)
df <- data.table(
groups = rep(seq(1, 4, 1),100),
measurement = runif(400)
)
cormat <- matrix(ncol=length(unique(df$groups)), nrow=length(unique(df$groups)))
for (i in unique(df$groups)){
    for (j in unique(df$groups)){
    cormat[i,j] <- cor(df[groups==i,]$measurement, df[groups==j,]$measurement)
}}
I hate the nested loop above, and would like to preferably find a dplyr/tidyverse approach my problem.
The expected output is:
> cormat
           [,1]        [,2]        [,3]        [,4]
[1,]  1.0000000 -0.10934904 -0.15159825  0.13237094
[2,] -0.1093490  1.00000000 -0.04278137 -0.02945215
[3,] -0.1515983 -0.04278137  1.00000000  0.04203516
[4,]  0.1323709 -0.02945215  0.04203516  1.00000000
(apologies if this question has been asked before, I was struggling to find a good search term)