user2543095 user2543095 - 3 months ago 19
R Question

creating a matrix from a smaller "key" matrix

let's say that I have a smaller, correlation matrix ("the key"):

a b c
a 1.0 0.0 0.2
b 0.0 1.0 0.1
c 0.2 0.1 1.0


I want to use this key on another data frame/matrix, with ID's (a, b, c), to make a larger matrix.

For example, this example data frame:

sex age id
F 20 a
F 22 b
M 23 b
M 18 a
F 12 c


would turn into this larger correlation matrix using the above "key":

a b b a c
a 1.0 0.0 0.0 1.0 0.2
b 0.0 1.0 1.0 0.0 0.1
b 0.0 1.0 1.0 0.0 0.1
a 1.0 0.0 0.0 1.0 0.2
c 0.2 0.1 0.1 0.2 1.0


what's a way to do this, and what's a way to do this computationally efficient, given a much larger data frame?

Answer

R allows subsetting by names:

mat_cor[df1$id,df1$id]
    a   b   b   a   c
a 1.0 0.0 0.0 1.0 0.2
b 0.0 1.0 1.0 0.0 0.1
b 0.0 1.0 1.0 0.0 0.1
a 1.0 0.0 0.0 1.0 0.2
c 0.2 0.1 0.1 0.2 1.0

Data

df1 <- structure(list(sex = structure(c(1L, 1L, 2L, 2L, 1L), .Label = c("F", 
                                                                 "M"), class = "factor"), age = c(20L, 22L, 23L, 18L, 12L), id = structure(c(1L, 
                                                                                                                                             2L, 2L, 1L, 3L), .Label = c("a", "b", "c"), class = "factor")), .Names = c("sex", 
                                                                                                                                                                                                                        "age", "id"), class = "data.frame", row.names = c(NA, -5L))
mat_cor <- structure(c(1, 0, 0.2, 0, 1, 0.1, 0.2, 0.1, 1), .Dim = c(3L, 
                                                         3L), .Dimnames = list(c("a", "b", "c"), c("a", "b", "c")))