Функция для сложения вложенных матриц вместе - PullRequest
1 голос
/ 14 июля 2020

У меня есть список под названием «Переходы», в котором есть результаты для групп трех размеров: малых, средних и больших. В каждой размерной группе есть несколько матриц. Вложенный элемент, который меня интересует, это Transitions $ "Size Group" $ nij.

Я хотел бы написать функцию, которая берет все три элемента размера и соответствующие им элементы матрицы nij и складывает их вместе.

Что-то похожее на Reduce('+', List of Matrices)

Я вручную собрал матрицы nij и составил список, чтобы показать, что у меня есть.

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"))

Каждая матрица в списке выше была вложена. Чтобы получить их, я сделал следующее:

Transitions.ls <- list(Transitions$Small$nij, Transitions$Medium$nij,Transitions$Large$nij)

Мой желаемый результат:

Reduce('+', Transitions.ls)
            j
i            Downgraded Unchanged Upgraded
  Downgraded        124       198      401
  Unchanged         276       389      197
  Upgraded          333       284      119

Структура данных исходного списка Переходы:

    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 = .)

1 Ответ

2 голосов
/ 14 июля 2020

Возможно, мы можем l oop над вектором "Size Group", извлечь элементы с помощью [[, а затем извлечь 'nij' (здесь можно использовать $ или [[), затем применить Reduce

v1 <- c("Small", "Medium", "Large")
Reduce(`+`, lapply(v1, function(nm) Transitions[[nm]]$nij))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...