如何动态访问数据帧列表中的特定属性 [英] How To Dynamically Access Specific Attribute In A List of Data Frames

查看:46
本文介绍了如何动态访问数据帧列表中的特定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据,它是数据帧的列表.我正在尝试访问列表中每个数据框内的特定属性.可以使用以下代码提取特定属性DP.UniqueId.

I have a data which is a list of data frames. I am trying to access a specific attribute inside each of the data frames in the list. It's possible to extract the specific attribute DP.UniqueId using below code.

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

此方法的问题在于它不是动态的.另外,我拥有的大数据列表中有成千上万个数据帧,对于每个数据帧,我都想提取此特定的DP.UniqueId属性.

Problem with this approach is that it's not dynamic. Also, the big data I have has thousands of data frames in the list and for each of the data frames I want to extract this specific DP.UniqueId attribute.

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

If I have to apply lapply() or for loop() to get this attribute dynamically, then I will have to reference it as:

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

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

But it looks like in R, attr() can't be referenced like above code does. Is there a way to extract specific attributes dynamically and store it in a data frame?

数据

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

推荐答案

似乎可以使用purrr包轻松完成此操作.例如

It seems like you can accomplish this easily with the purrr package. For example

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

map()将遍历初始列表,然后pluck()可以处理深度提取.

The map() will iterate over the initial list, and then pluck() can take care of the deep extraction.

这篇关于如何动态访问数据帧列表中的特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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