根据向量中的内容对列表进行分组

时间:2020-07-16 05:16:40

标签: r list matrix

我有一个称为In_Period的日期向量。我使用此向量从矩阵列表中划分出年份范围的子集。但是,当我设置子集时,将$ NA引入列表的第一个元素。这是因为矩阵列表可能没有年份名称作为元素。

data_out[as.character(In_Period)]
>
$<NA>
NULL

$`1997`
     j
i     Aaa Aa  A Baa Ba  B Caa
  Aaa   5  0  3   0  0  0   0
  Aa    5 26  4   2  0  0   0
  A     1 18 14   5  0  0   0
  Baa   0  4 16   8  5  0   2
  Ba    0  1  3  12  4  1   0
  B     0  1  0   1  3  2   0
  Caa   0  0  0   0  1  0   0

如何使用名称向量将列表中的命名元素正确子集化?

包含日期的向量:

In_Period <- c(1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013)

这是按日期命名的矩阵的列表:

list(`1997` = structure(c(5L, 5L, 1L, 0L, 0L, 0L, 0L, 0L, 26L, 
18L, 4L, 1L, 1L, 0L, 3L, 4L, 14L, 16L, 3L, 0L, 0L, 0L, 2L, 5L, 
8L, 12L, 1L, 0L, 0L, 0L, 0L, 5L, 4L, 3L, 1L, 0L, 0L, 0L, 0L, 
1L, 2L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L), .Dim = c(7L, 7L), .Dimnames = list(
    i = c("Aaa", "Aa", "A", "Baa", "Ba", "B", "Caa"), j = c("Aaa", 
    "Aa", "A", "Baa", "Ba", "B", "Caa")), class = c("xtabs", 
"table"), call = xtabs(formula = n ~ i + j, data = .)), `1998` = structure(c(6L, 
4L, 2L, 1L, 0L, 0L, 0L, 2L, 20L, 5L, 5L, 0L, 0L, 0L, 2L, 12L, 
26L, 9L, 4L, 0L, 0L, 1L, 7L, 4L, 8L, 2L, 0L, 0L, 0L, 4L, 1L, 
6L, 4L, 1L, 1L, 0L, 1L, 1L, 1L, 4L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, 1L), .Dim = c(7L, 7L), .Dimnames = list(i = c("Aaa", "Aa", 
"A", "Baa", "Ba", "B", "Caa"), j = c("Aaa", "Aa", "A", "Baa", 
"Ba", "B", "Caa")), class = c("xtabs", "table"), call = xtabs(formula = n ~ 
    i + j, data = .)), `1999` = structure(c(6L, 2L, 0L, 0L, 0L, 
0L, 0L, 3L, 13L, 13L, 1L, 1L, 0L, 0L, 4L, 10L, 17L, 6L, 3L, 1L, 
0L, 1L, 4L, 13L, 9L, 3L, 3L, 0L, 0L, 3L, 6L, 5L, 4L, 1L, 0L, 
0L, 0L, 1L, 0L, 4L, 2L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 1L), .Dim = c(7L, 
7L), .Dimnames = list(i = c("Aaa", "Aa", "A", "Baa", "Ba", "B", 
"Caa"), j = c("Aaa", "Aa", "A", "Baa", "Ba", "B", "Caa")), class = c("xtabs", 
"table"), call = xtabs(formula = n ~ i + j, data = .)), `2000` = structure(c(2L, 
1L, 2L, 0L, 0L, 0L, 0L, 4L, 10L, 7L, 2L, 0L, 0L, 0L, 1L, 10L, 
17L, 13L, 1L, 1L, 0L, 0L, 7L, 13L, 9L, 8L, 0L, 1L, 1L, 2L, 5L, 
10L, 5L, 1L, 1L, 0L, 0L, 0L, 2L, 2L, 3L, 0L, 0L, 0L, 0L, 0L, 
1L, 3L, 2L), .Dim = c(7L, 7L), .Dimnames = list(i = c("Aaa", 
"Aa", "A", "Baa", "Ba", "B", "Caa"), j = c("Aaa", "Aa", "A", 
"Baa", "Ba", "B", "Caa")), class = c("xtabs", "table"), call = xtabs(formula = n ~ 
    i + j, data = .)), `2001` = structure(c(1L, 3L, 0L, 0L, 0L, 
0L, 0L, 4L, 10L, 7L, 5L, 1L, 1L, 0L, 0L, 6L, 20L, 9L, 0L, 0L, 
0L, 0L, 2L, 14L, 11L, 7L, 0L, 0L, 0L, 2L, 2L, 12L, 8L, 4L, 1L, 
0L, 0L, 0L, 0L, 6L, 2L, 3L, 0L, 0L, 1L, 2L, 1L, 0L, 2L), .Dim = c(7L, 
7L), .Dimnames = list(i = c("Aaa", "Aa", "A", "Baa", "Ba", "B", 
"Caa"), j = c("Aaa", "Aa", "A", "Baa", "Ba", "B", "Caa")), class = c("xtabs", 
"table"), call = xtabs(formula = n ~ i + j, data = .)), `2002` = structure(c(2L, 
4L, 0L, 0L, 0L, 0L, 0L, 2L, 12L, 11L, 3L, 0L, 0L, 0L, 0L, 4L, 
15L, 11L, 3L, 0L, 0L, 0L, 8L, 7L, 14L, 10L, 1L, 0L, 0L, 2L, 2L, 
7L, 7L, 6L, 4L, 0L, 0L, 0L, 1L, 6L, 3L, 0L, 0L, 0L, 0L, 0L, 2L, 
1L, 2L), .Dim = c(7L, 7L), .Dimnames = list(i = c("Aaa", "Aa", 
"A", "Baa", "Ba", "B", "Caa"), j = c("Aaa", "Aa", "A", "Baa", 
"Ba", "B", "Caa")), class = c("xtabs", "table"), call = xtabs(formula = n ~ 
    i + j, data = .)), `2003` = structure(c(1L, 1L, 1L, 0L, 0L, 
0L, 0L, 4L, 12L, 7L, 6L, 0L, 0L, 0L, 1L, 8L, 15L, 8L, 3L, 0L, 
0L, 0L, 3L, 4L, 11L, 5L, 2L, 2L, 0L, 0L, 1L, 2L, 7L, 1L, 1L, 
0L, 2L, 1L, 11L, 8L, 4L, 0L, 0L, 0L, 1L, 1L, 3L, 3L, 2L), .Dim = c(7L, 
7L), .Dimnames = list(i = c("Aaa", "Aa", "A", "Baa", "Ba", "B", 
"Caa"), j = c("Aaa", "Aa", "A", "Baa", "Ba", "B", "Caa")), class = c("xtabs", 
"table"), call = xtabs(formula = n ~ i + j, data = .)), `2004` = structure(c(1L, 
5L, 4L, 0L, 0L, 0L, 0L, 2L, 17L, 8L, 6L, 2L, 2L, 0L, 0L, 3L, 
14L, 7L, 4L, 2L, 0L, 0L, 3L, 8L, 9L, 3L, 7L, 0L, 0L, 2L, 1L, 
3L, 5L, 4L, 4L, 0L, 0L, 1L, 2L, 1L, 6L, 4L, 0L, 0L, 0L, 0L, 0L, 
0L, 2L), .Dim = c(7L, 7L), .Dimnames = list(i = c("Aaa", "Aa", 
"A", "Baa", "Ba", "B", "Caa"), j = c("Aaa", "Aa", "A", "Baa", 
"Ba", "B", "Caa")), class = c("xtabs", "table"), call = xtabs(formula = n ~ 
    i + j, data = .)), `2005` = structure(c(4L, 0L, 0L, 0L, 0L, 
0L, 0L, 6L, 16L, 6L, 2L, 0L, 0L, 0L, 0L, 10L, 15L, 12L, 6L, 2L, 
0L, 1L, 7L, 9L, 12L, 4L, 2L, 0L, 0L, 2L, 3L, 5L, 8L, 3L, 0L, 
0L, 0L, 0L, 2L, 2L, 5L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), .Dim = c(7L, 
7L), .Dimnames = list(i = c("Aaa", "Aa", "A", "Baa", "Ba", "B", 
"Caa"), j = c("Aaa", "Aa", "A", "Baa", "Ba", "B", "Caa")), class = c("xtabs", 
"table"), call = xtabs(formula = n ~ i + j, data = .)), `2006` = structure(c(2L, 
3L, 1L, 0L, 0L, 0L, 0L, 2L, 11L, 6L, 3L, 1L, 0L, 0L, 1L, 10L, 
21L, 4L, 1L, 0L, 0L, 0L, 3L, 12L, 15L, 5L, 1L, 0L, 0L, 2L, 4L, 
8L, 9L, 5L, 0L, 0L, 1L, 2L, 5L, 3L, 3L, 1L, 0L, 0L, 0L, 0L, 1L, 
2L, 0L), .Dim = c(7L, 7L), .Dimnames = list(i = c("Aaa", "Aa", 
"A", "Baa", "Ba", "B", "Caa"), j = c("Aaa", "Aa", "A", "Baa", 
"Ba", "B", "Caa")), class = c("xtabs", "table"), call = xtabs(formula = n ~ 
    i + j, data = .)), `2007` = structure(c(2L, 2L, 0L, 0L, 0L, 
0L, 0L, 3L, 8L, 5L, 1L, 0L, 0L, 0L, 1L, 6L, 13L, 8L, 1L, 2L, 
0L, 0L, 1L, 10L, 12L, 6L, 0L, 1L, 0L, 1L, 6L, 11L, 12L, 4L, 0L, 
0L, 3L, 1L, 4L, 6L, 8L, 2L, 0L, 0L, 0L, 0L, 2L, 1L, 0L), .Dim = c(7L, 
7L), .Dimnames = list(i = c("Aaa", "Aa", "A", "Baa", "Ba", "B", 
"Caa"), j = c("Aaa", "Aa", "A", "Baa", "Ba", "B", "Caa")), class = c("xtabs", 
"table"), call = xtabs(formula = n ~ i + j, data = .)), `2008` = structure(c(1L, 
0L, 1L, 0L, 0L, 0L, 0L, 1L, 7L, 2L, 0L, 0L, 1L, 0L, 0L, 7L, 8L, 
4L, 1L, 2L, 0L, 1L, 2L, 15L, 9L, 9L, 4L, 1L, 0L, 1L, 5L, 14L, 
10L, 5L, 0L, 0L, 0L, 0L, 3L, 7L, 9L, 1L, 0L, 0L, 0L, 0L, 1L, 
2L, 0L), .Dim = c(7L, 7L), .Dimnames = list(i = c("Aaa", "Aa", 
"A", "Baa", "Ba", "B", "Caa"), j = c("Aaa", "Aa", "A", "Baa", 
"Ba", "B", "Caa")), class = c("xtabs", "table"), call = xtabs(formula = n ~ 
    i + j, data = .)), `2009` = structure(c(1L, 4L, 3L, 7L, 0L, 
0L, 0L, 0L, 4L, 13L, 16L, 13L, 6L, 0L, 1L, 3L, 7L, 8L, 8L, 10L, 
1L, 0L, 1L, 0L, 9L, 7L, 2L, 0L, 0L, 1L, 1L, 3L, 4L, 3L, 0L, 0L, 
0L, 1L, 1L, 2L, 1L, 0L, 0L, 0L, 0L, 2L, 1L, 1L, 1L), .Dim = c(7L, 
7L), .Dimnames = list(i = c("Aaa", "Aa", "A", "Baa", "Ba", "B", 
"Caa"), j = c("Aaa", "Aa", "A", "Baa", "Ba", "B", "Caa")), class = c("xtabs", 
"table"), call = xtabs(formula = n ~ i + j, data = .)), `2010` = structure(c(4L, 
3L, 0L, 0L, 0L, 0L, 0L, 4L, 31L, 11L, 2L, 3L, 0L, 1L, 5L, 12L, 
14L, 4L, 2L, 1L, 0L, 0L, 2L, 11L, 9L, 2L, 0L, 0L, 0L, 1L, 4L, 
4L, 3L, 2L, 0L, 0L, 1L, 1L, 1L, 1L, 3L, 3L, 0L, 0L, 0L, 0L, 0L, 
1L, 1L), .Dim = c(7L, 7L), .Dimnames = list(i = c("Aaa", "Aa", 
"A", "Baa", "Ba", "B", "Caa"), j = c("Aaa", "Aa", "A", "Baa", 
"Ba", "B", "Caa")), class = c("xtabs", "table"), call = xtabs(formula = n ~ 
    i + j, data = .)), `2011` = structure(c(1L, 0L, 0L, 0L, 0L, 
0L, 0L, 3L, 24L, 3L, 1L, 0L, 0L, 0L, 1L, 12L, 12L, 8L, 2L, 0L, 
0L, 2L, 6L, 13L, 6L, 5L, 3L, 0L, 0L, 8L, 7L, 6L, 4L, 2L, 1L, 
0L, 2L, 2L, 3L, 2L, 3L, 1L, 0L, 0L, 0L, 0L, 0L, 2L, 0L), .Dim = c(7L, 
7L), .Dimnames = list(i = c("Aaa", "Aa", "A", "Baa", "Ba", "B", 
"Caa"), j = c("Aaa", "Aa", "A", "Baa", "Ba", "B", "Caa")), class = c("xtabs", 
"table"), call = xtabs(formula = n ~ i + j, data = .)), `2012` = structure(c(0L, 
1L, 0L, 0L, 0L, 0L, 0L, 0L, 26L, 12L, 4L, 3L, 0L, 0L, 1L, 3L, 
17L, 15L, 7L, 1L, 0L, 0L, 1L, 5L, 14L, 11L, 3L, 0L, 0L, 1L, 0L, 
4L, 4L, 5L, 0L, 0L, 0L, 1L, 2L, 1L, 2L, 2L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L), .Dim = c(7L, 7L), .Dimnames = list(i = c("Aaa", "Aa", 
"A", "Baa", "Ba", "B", "Caa"), j = c("Aaa", "Aa", "A", "Baa", 
"Ba", "B", "Caa")), class = c("xtabs", "table"), call = xtabs(formula = n ~ 
    i + j, data = .)), `2013` = structure(c(0L, 4L, 0L, 0L, 0L, 
0L, 0L, 0L, 29L, 19L, 6L, 2L, 1L, 0L, 1L, 7L, 21L, 18L, 4L, 0L, 
0L, 0L, 3L, 5L, 10L, 2L, 1L, 0L, 0L, 0L, 0L, 2L, 3L, 3L, 0L, 
0L, 0L, 0L, 1L, 2L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L), .Dim = c(7L, 
7L), .Dimnames = list(i = c("Aaa", "Aa", "A", "Baa", "Ba", "B", 
"Caa"), j = c("Aaa", "Aa", "A", "Baa", "Ba", "B", "Caa")), class = c("xtabs", 
"table"), call = xtabs(formula = n ~ i + j, data = .)), `2014` = structure(c(0L, 
4L, 1L, 0L, 0L, 0L, 0L, 2L, 38L, 9L, 2L, 1L, 0L, 0L, 1L, 12L, 
21L, 2L, 0L, 0L, 0L, 0L, 1L, 15L, 12L, 3L, 2L, 0L, 0L, 0L, 5L, 
3L, 4L, 1L, 1L, 0L, 0L, 0L, 2L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 1L), .Dim = c(7L, 7L), .Dimnames = list(i = c("Aaa", "Aa", 
"A", "Baa", "Ba", "B", "Caa"), j = c("Aaa", "Aa", "A", "Baa", 
"Ba", "B", "Caa")), class = c("xtabs", "table"), call = xtabs(formula = n ~ 
    i + j, data = .)))

1 个答案:

答案 0 :(得分:0)

您应该能够进行字母索引。

 Name_of_list[ as.character(In_period] )

仅使用索引而不强制转换为数字将失败。我想您也可以尝试与列表的names()匹配。但这似乎有点巴洛克式。

如果要避免使用NA,请使用逻辑索引

 data_out[ names(data_out) %in% as.character( In_Period) ]