I am trying to create a correlation matrix with a for loop or another more simple/efficient solution, but to be honest I am really struggling it with it.
This is the structure of the data frame and the correlation matrix:
# Create data frame
ID <- c("r1", "r1", "r1", "r1", "r1", "r2", "r2", "r2", "r2", "r2", "r3", "r3", "r3", "r3", "r3")
V1.1 <- c(3, 3, 3, 3, 3, 3, 2, 3, 3, 1, 2, 2, 1, 1, 2)
V2.1 <- c(2, 2, 3, 2, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 3)
V3.1 <- c(4, 4, 4, 4, 3, 4, 1, 2, 5, 2, 2, 2, 4, 5, 1)
V4.1 <- c(3, 4, 3, 3, 3, 3, 3, 3, 4, 4, 5, 4, 4, 4, 2)
V5.1 <- c(3, 2, 3, 3, 2, 3, 2, 2, 2, 3, 2, 3, 3, 3, 3)
V1.2 <- c(4, 4, 3, 3, 4, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3)
V2.2 <- c(3, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5)
V3.2 <- c(2, 1, 2, 2, 2, 4, 3, 4, 4, 3, 4, 2, 1, 2, 1)
V4.2 <- c(2, 5, 2, 3, 4, 4, 3, 4, 2, 3, 4, 5, 2, 4, 3)
V5.2 <- c(5, 4, 3, 4, 3, 3, 4, 4, 2, 3, 2, 4, 4, 1, 3)
df <- data.frame(ID, V1.1, V2.1, V3.1, V4.1, V5.1, V1.2, V2.2, V3.2, V4.2, V5.2)
# Define variables
ID.vars <- c("r1", "r2", "r3")
vars <- c("V1", "V2", "V3", "V4", "V5")
vars.1 <- c("V1.1", "V2.1", "V3.1", "V4.1", "V5.1")
vars.2 <- c("V1.2", "V2.2", "V3.2", "V4.2", "V5.2")
# Empty data frame for correlation matrix
corrmat <- data.frame(ID = ID.vars)
corrmat <- cbind(corrmat, matrix(NA, nrow = length(ID.vars), ncol = length(vars)))
names(corrmat)[2:ncol(corrmat)] <- vars
This is what I want to do with a loop or better solution:
# Subset data per id
r1 <- subset(df, ID == "r1", select = c(vars.1, vars.2))
r2 <- subset(df, ID == "r2", select = c(vars.1, vars.2))
r3 <- subset(df, ID == "r3", select = c(vars.1, vars.2))
# Fill-in correlations matrix
corrmat[corrmat$ID == "r1", "V1"] <- cor(r1$V1.1, r1$V1.2)
corrmat[corrmat$ID == "r1", "V2"] <- cor(r1$V2.1, r1$V2.2)
corrmat[corrmat$ID == "r1", "V3"] <- cor(r1$V3.1, r1$V3.2)
corrmat[corrmat$ID == "r1", "V4"] <- cor(r1$V4.1, r1$V4.2)
corrmat[corrmat$ID == "r1", "V5"] <- cor(r1$V5.1, r1$V5.2)
corrmat[corrmat$ID == "r2", "V1"] <- cor(r2$V1.1, r2$V1.2)
corrmat[corrmat$ID == "r2", "V2"] <- cor(r2$V2.1, r2$V2.2)
corrmat[corrmat$ID == "r2", "V3"] <- cor(r2$V3.1, r2$V3.2)
corrmat[corrmat$ID == "r2", "V4"] <- cor(r2$V4.1, r2$V4.2)
corrmat[corrmat$ID == "r2", "V5"] <- cor(r2$V5.1, r2$V5.2)
corrmat[corrmat$ID == "r3", "V1"] <- cor(r3$V1.1, r3$V1.2)
corrmat[corrmat$ID == "r3", "V2"] <- cor(r3$V2.1, r3$V2.2)
corrmat[corrmat$ID == "r3", "V3"] <- cor(r3$V3.1, r3$V3.2)
corrmat[corrmat$ID == "r3", "V4"] <- cor(r3$V4.1, r3$V4.2)
corrmat[corrmat$ID == "r3", "V5"] <- cor(r3$V5.1, r3$V5.2)
This is the beginning of the attempt and now I am stuck :(
# Loop attempt
for(i in ID.vars) {
results <- subset(df, ID == i, select = c(vars.1, vars.2)) # subset data
# Loop across variables
for(j in vars){
}
}
Thanks!