【问题标题】:How to assign list attributes as list names in R如何在 R 中将列表属性分配为列表名称
【发布时间】:2019-05-30 12:31:07
【问题描述】:

我有一个列表(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"

这是我为tables_list 中的每个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)
  })

我想不出一种方法将这些属性分配为它们各自列表的名称。有没有人知道如何解决这个问题?

【问题讨论】:

    标签: r list attributes names


    【解决方案1】:

    如果我理解正确,下面将属性"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
      })
    })
    

    【讨论】:

    • 我尝试将您的代码示例应用到我的代码中,但是,当涉及到我的示例中的 tables_list[[17]] 之类的列表时,我收到以下错误:Error in names(L) &lt;- sapply(L, attr, "caption") : 'names' attribute [2] must be the same length as the vector [1]
    • 你能发布dput(tables_list[16:18])的输出吗?或者,如果它太长,只需dput(tables_list[17])。 (仅限[]。)
    • 另外,这个错误信息可能意味着tables_list[[17]]的某些成员没有设置标题属性。
    猜你喜欢
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2022-01-13
    • 2012-11-06
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    相关资源
    最近更新 更多