Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Created on 2023-03-30 with reprex v2.0.2

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 an other.

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

Created on 2023-03-30 with reprex v2.0.2


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)

Created on 2023-03-30 with reprex v2.0.2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading