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

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

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

问题

  1. 我有一个对象列表(调查::svyciprop的输出),我试图提取它们的属性以创建一个包含所有结果的数据框。
  2. ```R
  3. # 一个对象的示例
  4. props_and_cis[[1]]
  5. 2.5% 97.5%
  6. var1 0.932 0.826 0.98

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

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

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

  1. tribble(
  2. ~variable, ~prop, ~ci_lower, ~ci_upper,
  3. var, prop, ci_lower, ci_upper
  4. )

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

ETA:

  1. > dput(props_and_cis[[1]])
  2. structure(c(var1 = 0.932403111115339), var = structure(0.00119910004765771, dim = c(1L,
  3. 1L), dimnames = list("as.numeric(var1)", "as.numeric(var1)")), ci = c(`2.5%` = 0.825647967272783,
  4. `97.5%` = 0.975715067477937), class = "svyciprop")
  1. <details>
  2. <summary>英文:</summary>
  3. 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

  1. 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]

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

  1. But I can&#39;t seem to make it work. Can someone help?
  2. 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")

  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. ``` r
  5. # 提取所需数据的函数
  6. extract_attr <- function(x) {
  7. v <- attr(x, "names")
  8. ci <- attr(x, "ci")
  9. y <- cbind(data.frame(var = v, prop = c(x)), as.data.frame(t(ci)))
  10. row.names(y) <- NULL
  11. y
  12. }
  13. # 对列表成员应用函数并合并结果为一个 data.frame
  14. res <- lapply(props_and_cis, extract_attr)
  15. do.call(rbind, res)
  1. # 数据
  2. posted <- structure(c(var1 = 0.932403111115339),
  3. var = structure(0.00119910004765771, dim = c(1L, 1L),
  4. dimnames = list("as.numeric(var1)", "as.numeric(var1)")),
  5. ci = c(`2.5%` = 0.825647967272783, `97.5%` = 0.975715067477937),
  6. class = "svyciprop")
  7. props_and_cis <- list(posted, posted, posted)
英文:

Write a function to extract the wanted data.

  1. extract_attr &lt;- function(x) {
  2. v &lt;- attr(x, &quot;names&quot;)
  3. ci &lt;- attr(x, &quot;ci&quot;)
  4. y &lt;- cbind(data.frame(var = v, prop = c(x)), as.data.frame(t(ci)))
  5. row.names(y) &lt;- NULL
  6. y
  7. }
  8. extract_attr(props_and_cis[[1]])
  9. #&gt; var prop 2.5% 97.5%
  10. #&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.

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

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


Data

  1. posted &lt;- structure(c(var1 = 0.932403111115339),
  2. var = structure(0.00119910004765771, dim = c(1L, 1L),
  3. dimnames = list(&quot;as.numeric(var1)&quot;, &quot;as.numeric(var1)&quot;)),
  4. ci = c(`2.5%` = 0.825647967272783, `97.5%` = 0.975715067477937),
  5. class = &quot;svyciprop&quot;)
  6. 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:

确定