英文:
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'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 <- 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
}
extract_attr(props_and_cis[[1]])
#> var prop 2.5% 97.5%
#> 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 <- lapply(props_and_cis, extract_attr)
do.call(rbind, res)
#> var prop 2.5% 97.5%
#> 1 var1 0.9324031 0.825648 0.9757151
#> 2 var1 0.9324031 0.825648 0.9757151
#> 3 var1 0.9324031 0.825648 0.9757151
<sup>Created on 2023-03-30 with reprex v2.0.2</sup>
Data
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)
<sup>Created on 2023-03-30 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论