I have a list called Transitions that has results for 3 size groups: Small, Medium, and Large. In each size group are multiple matrices. The nested element I'm interested in is Transitions$"Size Group"$nij.
I would like to write a function that takes all three size elements and their respective nij matrix elements and adds them together.
Something similar to Reduce('+', List of Matrices)
I manually collected the nij matrices and made a list to show what I have.
Transitions.ls <-
list(Small = structure(c(38L, 83L, 112L, 58L, 142L, 83L, 124L,
62L, 23L), .Dim = c(3L, 3L), .Dimnames = list(i = c("Downgraded",
"Unchanged", "Upgraded"), j = c("Downgraded", "Unchanged", "Upgraded"
)), class = "table"), Medium = structure(c(83L, 184L, 210L, 134L,
243L, 183L, 256L, 122L, 86L), .Dim = c(3L, 3L), .Dimnames = list(
i = c("Downgraded", "Unchanged", "Upgraded"), j = c("Downgraded",
"Unchanged", "Upgraded")), class = "table"), Large = structure(c(3L,
9L, 11L, 6L, 4L, 18L, 21L, 13L, 10L), .Dim = c(3L, 3L), .Dimnames = list(
i = c("Downgraded", "Unchanged", "Upgraded"), j = c("Downgraded",
"Unchanged", "Upgraded")), class = "table"))
Each matrix in the list above was nested. I did the following to obtain them:
Transitions.ls <- list(Transitions$Small$nij, Transitions$Medium$nij,Transitions$Large$nij)
My desired output is:
Reduce('+', Transitions.ls)
j
i Downgraded Unchanged Upgraded
Downgraded 124 198 401
Unchanged 276 389 197
Upgraded 333 284 119
Data structure of the original list Transitions:
List of 3
$ Small :List of 12
..$ nij : 'table' int [1:3, 1:3] 38 83 112 58 142 83 124 62 23
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
..$ ni : Named num [1:3] 220 287 218
.. ..- attr(*, "names")= chr [1:3] "Downgraded" "Unchanged" "Upgraded"
..$ pij : 'table' num [1:3, 1:3] 0.173 0.289 0.514 0.264 0.495 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
..$ pj : Named num [1:3] 0.321 0.39 0.288
.. ..- attr(*, "names")= chr [1:3] "Downgraded" "Unchanged" "Upgraded"
..$ se.pij : 'table' num [1:3, 1:3] 0.0255 0.0268 0.0339 0.0297 0.0295 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
..$ nij.out : 'xtabs' int [1:3, 1:3] 1 3 3 1 8 4 2 1 1
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
..$ ni.out : Named num [1:3] 4 12 8
.. ..- attr(*, "names")= chr [1:3] "Downgraded" "Unchanged" "Upgraded"
..$ pij.out : 'xtabs' num [1:3, 1:3] 0.25 0.25 0.375 0.25 0.667 ...
.. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
..$ pj.out : Named num [1:3] 0.292 0.542 0.167
.. ..- attr(*, "names")= chr [1:3] "Downgraded" "Unchanged" "Upgraded"
..$ se.pij.out : 'xtabs' num [1:3, 1:3] 0.217 0.125 0.171 0.217 0.136 ...
.. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
..$ p.diff : 'table' num [1:3, 1:3] -0.0773 0.0392 0.1388 0.0136 -0.1719 ...
.. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
..$ Transitions:List of 16
.. ..$ 1998: 'xtabs' int [1:3, 1:3] 2 10 12 5 11 6 7 6 2
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 1999: 'xtabs' int [1:3, 1:3] 4 9 8 6 8 5 14 6 2
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 2000: 'xtabs' int [1:3, 1:3] 5 8 12 6 10 8 10 4 4
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 2001: 'xtabs' int [1:3, 1:3] 6 5 10 8 12 5 9 7 3
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 2002: 'xtabs' int [1:3, 1:3] 4 8 10 6 12 7 10 7 3
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 2003: 'xtabs' int [1:3, 1:3] 3 8 12 4 6 7 13 6 0
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 2004: 'xtabs' int [1:3, 1:3] 1 4 7 5 5 10 12 6 0
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 2005: 'xtabs' int [1:3, 1:3] 1 5 6 3 11 8 8 3 2
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 2006: 'xtabs' int [1:3, 1:3] 4 7 7 1 13 4 6 2 2
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 2007: 'xtabs' int [1:3, 1:3] 3 2 4 4 10 3 9 2 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 2008: 'xtabs' int [1:3, 1:3] 1 3 3 2 8 5 3 2 0
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 2009: 'xtabs' int [1:3, 1:3] 1 5 4 2 5 0 4 5 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 2010: 'xtabs' int [1:3, 1:3] 1 2 8 4 4 4 6 1 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 2011: 'xtabs' int [1:3, 1:3] 1 3 3 1 9 4 6 2 0
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 2012: 'xtabs' int [1:3, 1:3] 0 1 3 0 10 3 5 2 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
.. ..$ 2013: 'xtabs' int [1:3, 1:3] 1 3 3 1 8 4 2 1 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ i: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. .. ..$ j: chr [1:3] "Downgraded" "Unchanged" "Upgraded"
.. .. ..- attr(*, "call")= language xtabs(formula = n ~ i + j, data = .)
lapply(Transitions, function(x) Reduce('+', x))
– akrunTransitions
is not working. Is the orignal objectTransitions
instead ofTransitions.ls
– akrunstr
.. Not clear which way it is nested – akrun