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

如何将特定属性作为新行添加到列表中的各个数据帧中

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

    我有一个数据帧列表,在其中我想读取一个特定的属性,然后将它作为第一行附加到该属性所属的各个数据帧中。

    在下面的数据中,我想阅读 DP.UniqueId 动态的,可以这样做,但不确定如何将其附加为数据帧的第一行, 杜仲 这样我就可以得到如示例输出中所示的输出。

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

    以上代码信用 @MrFlick 作为答案 this question .

    我想动态地做这件事,因为列表中有数千个数据帧。

    数据

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

    样本输出

    $A
           AA
    1 A-024
    2 5.49485
    
    $B
              BB
    1 B-025
    2 0.08707662
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   A. Suliman    6 年前

    我们可以用R基

    lapply(new_data, function(x) rbind(attr(x[[1]],"SpotfireColumnMetaData")$DP.UniqueId,x))
    
    $A
           AA
    1   A-024
    2 5.49485
    
    $B 
             BB
    1      B-025
    2 0.08707662