从列表中提取对象的属性并将它们写入数据框中。

huangapple go评论66阅读模式
英文:

Extract attributes from objects in a list and write them into a dataframe

问题

我有一个对象列表(调查::svyciprop的输出),我试图提取它们的属性以创建一个包含所有结果的数据框。

```R
# 一个对象的示例
props_and_cis[[1]]
                2.5% 97.5%
var1     0.932 0.826  0.98

我已经找到了如何将我想要的每个项目提取到单独的列中:

var <- attr(props_and_cis[[1]],"names")
prop <- as.vector(props_and_cis[[1]])
ci_lower <- attr(props_and_cis[[1]], "ci")[1]
ci_upper <- attr(props_and_cis[[1]], "ci")[2]

我想遍历我的列表props_and_cis中的每个对象,并将提取的内容写入一个数据框中,例如:

tribble(
  ~variable, ~prop, ~ci_lower, ~ci_upper,
  var, prop, ci_lower, ci_upper
)

但是我似乎无法使其工作。有人可以帮忙吗?

ETA:

> dput(props_and_cis[[1]])
structure(c(var1 = 0.932403111115339), var = structure(0.00119910004765771, dim = c(1L, 
1L), dimnames = list("as.numeric(var1)", "as.numeric(var1)")), ci = c(`2.5%` = 0.825647967272783, 
`97.5%` = 0.975715067477937), class = "svyciprop")

<details>
<summary>英文:</summary>

I have a list of objects (outputs of survey::svyciprop) and I am trying to extract their attributes to create a data frame with all of the results.

#example of an object
props_and_cis[[1]]
2.5% 97.5%
var1 0.932 0.826 0.98


I have figured out how to extract each item I want in a separate column:

var <- attr(props_and_cis[[1]],"names")
prop <- as.vector(props_and_cis[[1]])
ci_lower <- attr(props_and_cis[[1]], "ci")[1]
ci_upper <- attr(props_and_cis[[1]], "ci")[2]


I would like to iterate through each object in my list `props_and_cis` and write the extracted content into a dataframe, for example:

tribble(
~variable, ~prop, ~ci_lower, ~ci_upper,
var,prop,ci_lower,ci_upper
)


But I can&#39;t seem to make it work. Can someone help?

ETA:

> dput(props_and_cis[[1]])
structure(c(var1 = 0.932403111115339), var = structure(0.00119910004765771, dim = c(1L,
1L), dimnames = list("as.numeric(var1)", "as.numeric(var1)")), ci = c(2.5% = 0.825647967272783,
97.5% = 0.975715067477937), class = "svyciprop")


</details>


# 答案1
**得分**: 1

``` r
# 提取所需数据的函数
extract_attr <- function(x) {
  v <- attr(x, "names")
  ci <- attr(x, "ci")
  y <- cbind(data.frame(var = v, prop = c(x)), as.data.frame(t(ci)))
  row.names(y) <- NULL
  y
}

# 对列表成员应用函数并合并结果为一个 data.frame
res <- lapply(props_and_cis, extract_attr)
do.call(rbind, res)
# 数据
posted <-   structure(c(var1 = 0.932403111115339), 
                      var = structure(0.00119910004765771, dim = c(1L, 1L),
                                      dimnames = list("as.numeric(var1)", "as.numeric(var1)")),
                      ci = c(`2.5%` = 0.825647967272783, `97.5%` = 0.975715067477937), 
                      class = "svyciprop")

props_and_cis <- list(posted, posted, posted)
英文:

Write a function to extract the wanted data.

extract_attr &lt;- function(x) {
  v &lt;- attr(x, &quot;names&quot;)
  ci &lt;- attr(x, &quot;ci&quot;)
  y &lt;- cbind(data.frame(var = v, prop = c(x)), as.data.frame(t(ci)))
  row.names(y) &lt;- NULL
  y
}

extract_attr(props_and_cis[[1]])
#&gt;    var      prop     2.5%     97.5%
#&gt; 1 var1 0.9324031 0.825648 0.9757151

<sup>Created on 2023-03-30 with reprex v2.0.2</sup>

Then lapply the function to your list members and rbind the result to get one data.frame. In the example below I repeat the posted data example, all list members are equal to one another.

res &lt;- lapply(props_and_cis, extract_attr)
do.call(rbind, res)
#&gt;    var      prop     2.5%     97.5%
#&gt; 1 var1 0.9324031 0.825648 0.9757151
#&gt; 2 var1 0.9324031 0.825648 0.9757151
#&gt; 3 var1 0.9324031 0.825648 0.9757151

<sup>Created on 2023-03-30 with reprex v2.0.2</sup>


Data

posted &lt;-   structure(c(var1 = 0.932403111115339), 
                      var = structure(0.00119910004765771, dim = c(1L, 1L),
                                      dimnames = list(&quot;as.numeric(var1)&quot;, &quot;as.numeric(var1)&quot;)),
                      ci = c(`2.5%` = 0.825647967272783, `97.5%` = 0.975715067477937), 
                      class = &quot;svyciprop&quot;)

props_and_cis &lt;- list(posted, posted, posted)

<sup>Created on 2023-03-30 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年3月31日 03:12:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定