如何在R中将列表属性分配为列表名称

时间:2019-05-30 12:31:07

标签: r list attributes names

我有一个列表(414个元素),其中包含其他不同长度的列表(范围从0到9)。每个子列表具有不同数量的行和列。

一些子列表的长度为1,如下所示,并且只有1个属性:

tables_list[[1]]
     [,1]                         [,2]                
[1,] "ID Number"                  "ABCD"              
[2,] "Code"                       "1239463"             
[3,] "Version"                    "1"                 
[4,] "Name"                       "ABC"
[5,] "Status"                     "Open"         
[6,] "Currency"                   "USD"               
[7,] "Average"                    "No"                
[8,] "FX Rate"                    "2.47"    

attr(,"caption")
[1] "5 && Introduction && NA"

其他子列表的长度为2或更高,并具有1个或多个属性,例如以下属性:

tables_list[[17]]
[[1]]
      [,1]  [,2]                                                  [,3]  [,4]              [,5]            [,6]              [,7]          [,8] [,9]            
 [1,] ""    ""                                                    "USD" "Balance"         "Movement in"   "Aggregate"       "Overall"     ""   "Overall"       
 [2,] ""    ""                                                    ""    "brought forward" "year"          "annual"          "aggregate"   ""   "funded account"
 [3,] ""    ""                                                    ""    "from previous"   ""              "information"    "adjustments"  ""   ""              
 [4,] ""    ""                                                    ""    "year end"        ""              ""                ""            ""   ""              
 [5,] ""    ""                                                    ""    "1"               "2"             "3"               "4"           ""   "5"             
 [6,] "12"  "Value 1"                                             ""    "0"               "3,275,020"     "3,275,020"       ""            "0"  "3,275,020"     
 [7,] "13"  "Value 2"                                             ""    "0"               "0"             "0"               ""            "0"  "0"             
 [8,] "14"  "Value 3"                                             ""    "0"               "8,267,862"     "8,267,862"       ""            "0"  "8,267,862"     
 [9,] "15"  "Value 4"                                             ""    "0"               "(590,073,321)" "(590,073,321)"   ""            "0"  "(590,073,321)" 
[10,] "16"  "Value 5"                                             ""    "0"               "0"             "0"               ""            "0"  "0"             
[11,] "17"  "Value 6"                                             ""    "0"               "0"             "0"               ""            "0"  "0"             
[12,] "18"  "Value 7"                                             ""    "0"               "0"             "0"               ""            "0"  "0"             
[13,] "19"  "Value 8"                                             ""    "0"               "0"             "0"               ""            "0"  "0"             
[14,] "20"  "Value 9"                                             ""    "0"               "(459,222,782)" "(459,222,782)"   ""            "0"  "(459,222,782)" 

[[2]]
     [,1]               [,2]   [,3]                                                                  [,4]           
[1,] "Theme"            "Year" "Comment"                                                             "Created"      
[2,] "Line 17 Column 2" "N/A"  "Amounts are calculated according to recent standards"                "XXXXXXXXXXXX"
[3,] ""                 ""     "paid by XXXXXXXXXXXXX"                                               ""      
attr(,"caption")
[1] "20 && Values for year 2017 && NA"
[2] "20 && Comments for 2017 && NA"

这是我的代码,用于为list中的每个tables_list分配属性。

tables_list <- lapply(tables$page, function(p) {
    cat(p, "\n")
    out <- extract_tables(Path, 
                          pages = p,
                          encoding = "UTF-8", 
                          method = "stream", 
                          output = "matrix")
    #This part of the code points out the title, page of each table and reporting year if applicable so we can keep track of what goes where
    attr(out, "caption") <- paste(as.character(tables$page[tables$page %in% p]), tables$text[tables$page %in% p], tables$Reportingyear[tables$page %in% p], sep = " && ")
    return(out)
  })

我无法找出一种方法来将这些属性分配为其各自列表的名称。有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,以下内容将属性"caption"的值分配给每个子列表。

tables_list <- lapply(tables_list, function(L){
  names(L) <- sapply(L, attr, "caption")
  L
})

tables_list[[2]]$V
#     [,1] [,2]
#[1,]    1    6
#[2,]    2    7
#[3,]    3    8
#[4,]    4    9
#[5,]    5   10
#attr(,"caption")
#[1] "V"

数据示例创建代码。

这将创建一个列表列表,每个列表均以矩阵作为成员。矩阵的属性"caption"设置为大写字母。

tables_list <- list(
  list(matrix(1:6, nrow = 3)),
  list(matrix(1:8, nrow = 4), matrix(1:10, nrow = 5)),
  list(matrix(1:4, nrow = 2), matrix(1:18, nrow = 9), matrix(4:1, nrow = 4))
)


set.seed(1234)
tables_list <- lapply(tables_list, function(L){
  lapply(L, function(M) {
    attr(M, "caption") <- sample(LETTERS, 1)
    M
  })
})