代码之家  ›  专栏  ›  技术社区  ›  Chetan Arvind Patil

如何动态访问数据帧列表中的特定属性

  •  2
  • Chetan Arvind Patil  · 技术社区  · 6 年前

    我有一个数据框架列表。我试图访问列表中每个数据帧内的特定属性。可以提取特定的属性 DP.UniqueId 使用下面的代码。

    > attr(new_data$A$AA, "SpotfireColumnMetaData")$DP.UniqueId
    [1] "A-024"
    

    这种方法的问题在于它不是动态的。此外,我拥有的大数据在列表中有数千个数据帧,对于每个数据帧,我都希望提取这个特定的 杜仲 属性。

    如果我必须申请 lapply() for loop() 要动态获取此属性,则必须将其引用为:

    > attr(new_data[1][1], "SpotfireColumnMetaData")$DP.UniqueId
    NULL
    

    但它看起来像 R , attr() 不能像上面的代码那样被引用。有没有一种方法可以动态提取特定的属性并将其存储在数据帧中?

    数据

    new_data <- list(A = structure(list(AA = structure(5.49485, SpotfireColumnMetaData = list(
      DP.TestNumber = "111", DP.Type = "", DP.TestName = "ABC", 
      DP.Info = "PTR", DP.TestUnit = "Mohm", DP.Statistic = "raw", 
      DP.Program = "", DP.ScaleFactor = 0L, DP.FilteredOutCells = 0L, 
      Limits.Prod.Lower = 2, Limits.Prod.Target = NaN, Limits.Prod.Upper = 7, 
      Limits.Spec.Lower = -Inf, Limits.Spec.Target = NaN, Limits.Spec.Upper = Inf, 
      Limits.Outlier.Lower = -Inf, Limits.Outlier.Target = NaN, 
      Limits.Outlier.Upper = Inf, Limits.Whatif.Lower = -Inf, Limits.Whatif.Target = NaN, 
      Limits.Whatif.Upper = Inf, DP.ParamType = "PARAMETRIC", DP.BlockId = "", 
      DP.Scratch = "", DP.ColumnId = "", Dp.BaseName = "", DP.FTR.testtxt = "", 
      DP.PTR.testtxt = "A  -1 <> B", DP.DTR.textdat = "", 
      DP.MPR.pinnum = "0", DP.UniqueId = "A-024"))), class = "data.frame", row.names = c(NA,-1L)),
      B = structure(list(BB = structure(0.08707662, SpotfireColumnMetaData = list(
      DP.TestNumber = "112", DP.Type = "", DP.TestName = "ABC", 
    DP.Info = "PTR", DP.TestUnit = "Mohm", DP.Statistic = "raw", 
    DP.Program = "", DP.ScaleFactor = 0L, DP.FilteredOutCells = 0L, 
    Limits.Prod.Lower = 2, Limits.Prod.Target = NaN, Limits.Prod.Upper = 7, 
    Limits.Spec.Lower = -Inf, Limits.Spec.Target = NaN, Limits.Spec.Upper = Inf, 
    Limits.Outlier.Lower = -Inf, Limits.Outlier.Target = NaN, 
    Limits.Outlier.Upper = Inf, Limits.Whatif.Lower = -Inf, Limits.Whatif.Target = NaN, 
    Limits.Whatif.Upper = Inf, DP.ParamType = "PARAMETRIC", DP.BlockId = "", 
    DP.Scratch = "", DP.ColumnId = "", Dp.BaseName = "", DP.FTR.testtxt = "", 
    DP.PTR.testtxt = "A  -1 <> B", DP.DTR.textdat = "", 
    DP.MPR.pinnum = "0", DP.UniqueId = "B-025"))), class = "data.frame", row.names = c(NA,-1L)))
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   MrFlick    6 年前

    似乎你可以用 purrr 包裹。例如

    library(purrr)
    new_data %>% map(pluck, 1, attr_getter("SpotfireColumnMetaData"), "DP.UniqueId")
    # $A
    # [1] "A-024"
    # $B
    # [1] "B-025"
    

    这个 map() 将遍历初始列表,然后 pluck() 可以处理深部萃取。