Let's say I have a time series and in each iteration, I take a fixed portion of it and calculate the correlation matrix. Also, assume three elements only, which are denoted with their names in the correlation matrix. I want to give them sequential numbers, meaning the first element is 1, second is 2 and so forth. Then I want to have a data frame in a way that expands these matrices. For example:
The first element is the element "from", the second one is "to", the third one is the correlation value and the fourth one is the time. I can give the times as input and repeat it twice many times as the elements. I realize that I will have duplicates for each correlation value, with a difference in "to" and "from" elements and that is what I am looking for. How can I construct this? Here is my data, where g.list
is a list of correlation matrices:
> dput(g.list)
list(structure(c(1, 0.352209944821856, 0.802051885793422, 0.352209944821857,
1, 0.827370298950111, 0.802051885793422, 0.827370298950111, 1
), .Dim = c(3L, 3L), .Dimnames = list(c("jpm", "gs", "ms"), c("jpm",
"gs", "ms"))), structure(c(1, 0.670163753398499, 0.753168359152204,
0.6701637533985, 1, 0, 0.753168359152202, 0, 1), .Dim = c(3L,
3L), .Dimnames = list(c("jpm", "gs", "ms"), c("jpm", "gs", "ms"
))), structure(c(1, 0.681190013681026, 0.153608963486821, 0.681190013681026,
1, 0.82058156983829, 0.153608963486822, 0.82058156983829, 1), .Dim = c(3L,
3L), .Dimnames = list(c("jpm", "gs", "ms"), c("jpm", "gs", "ms"
))))
CodePudding user response:
Are you looking for this ?
result <- do.call(rbind, Map(function(x, y)
cbind(which(x < 1, arr.ind = TRUE), value = tmp[tmp != 1], year = y),
g.list, 2018:2020))
result
# row col value year
#gs 2 1 0.352 2018
#ms 3 1 0.802 2018
#jpm 1 2 0.352 2018
#ms 3 2 0.827 2018
#jpm 1 3 0.802 2018
#gs 2 3 0.827 2018
#gs 2 1 0.352 2019
#ms 3 1 0.802 2019
#jpm 1 2 0.352 2019
#ms 3 2 0.827 2019
#jpm 1 3 0.802 2019
#gs 2 3 0.827 2019
#gs 2 1 0.352 2020
#ms 3 1 0.802 2020
#jpm 1 2 0.352 2020
#ms 3 2 0.827 2020
#jpm 1 3 0.802 2020
#gs 2 3 0.827 2020