如何将特定属性作为新行添加到列表中的各个数据框 [英] How To Add Specific Attribute As A New Row To Respective Data Frame In A List

查看:105
本文介绍了如何将特定属性作为新行添加到列表中的各个数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list of data frames in which I want to read a specific attribute and then append it as the first row the respective data frame to which that attribute belongs.

在下面的数据中,我想动态读取DP.UniqueId,可以按以下步骤进行操作,但是不确定如何将其附加为DP.UniqueId所属的数据帧的第一行,以便获得如图所示的输出样本输出.

In below data, I want to read DP.UniqueId dynamically which can be done as follows, but not sure how to append it as first row of the data frame that DP.UniqueId belongs to so that I get output as shown in sample output.

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

以上代码功劳 @MrFlick 作为我想动态地执行此操作,因为我拥有的大数据列表中有成千上万个数据帧.

This I want to do dynamically as the big data I have has thousands of data frames in the list.

数据

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

推荐答案

使用基数R我们可以做到

Using base R we can do

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

这篇关于如何将特定属性作为新行添加到列表中的各个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆