Search code examples
rdplyrtidyrsurvey

Extract elements from list to create data frame


library(survey)

I have data such as this. I am using the survey package to produce the MEAN, SE and FREQ of each variables in the vector named vars. I am new to manipulating lists in R & would really appreciate help!

df <- data.frame(sex = c('F', 'M', NA, 'M', 'M', 'M', 'F', 'F'),
                 married = c(1,1,1,1,0,0,1,1),
                 pens = c(0, 1, 1, NA, 1, 1, 0, 0),
                 weight = c(1.12, 0.55, 1.1, 0.6, 0.23, 0.23, 0.66, 0.67))

I run the following code to get a list with the mean, se and freq like this:

vars <- c("sex","married","pens")
design <- svydesign(ids=~1, data=df, weights=~weight)


myfun <- function(x){
  means <- svymean(as.formula(paste0('~interaction(', x, ')')), design, na.rm = T)
  table <- svytable(as.formula(paste0('~interaction(', x, ')')), design)
  results <- list(svymean = means, svytable = table)
  return(results)
}

lapply(vars, myfun)

The output looks like this:

[[1]]
[[1]]$svymean
                     mean     SE
interaction(sex)F 0.60345 0.2067
interaction(sex)M 0.39655 0.2067

[[1]]$svytable
interaction(sex)
   F    M 
2.45 1.61 


[[2]]
[[2]]$svymean
                          mean     SE
interaction(married)0 0.089147 0.0717
interaction(married)1 0.910853 0.0717

[[2]]$svytable
interaction(married)
   0    1 
0.46 4.70 


[[3]]
[[3]]$svymean
                      mean     SE
interaction(pens)0 0.53728 0.2255
interaction(pens)1 0.46272 0.2255

[[3]]$svytable
interaction(pens)
   0    1 
2.45 2.11 

I want to extract/manipulate this list above to create a dataframe that looks more like this:

    Var                mean     SE       freq
interaction(sex)F     0.60345 0.2067    2.45
interaction(sex)M     0.39655 0.2067    1.61
interaction(married)0 0.089147 0.0717    0.46
interaction(married)1 0.910853 0.0717     4.7

Is this possible?


Solution

  • You could decide to change your main function so as to run lapply only once:

    myfun <- function(x){
      form <- reformulate(sprintf('interaction(%s)', x))
      cbind(as.data.frame(svymean(form, design, na.rm = T)), freq = c(svytable(form, design)))
    }
    
    do.call(rbind, lapply(vars, myfun))