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

时间:2019-02-11 04:44:35

标签: r list dataframe attributes

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

在下面的数据中,我想动态读取DP.UniqueId,可以按以下步骤进行操作,但是不确定如何将其追加为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 个答案:

答案 0 :(得分:1)

使用基数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