R 将列表对象操纵到数据框中

R 将列表对象操纵到数据框中,r,dplyr,survey,R,Dplyr,Survey,我有这样的数据。我正在使用调查软件包生成名为vars的向量中每个变量的平均值、SE和FREQ。我是在R中操作列表的新手&非常感谢您的帮助 library(survey) 我想提取/操作上面的列表,以创建一个看起来更像这样的数据帧: 如您所见,sum_svytable是$svytable generated列表中为每个变量生成的频率之和。尽管在我的示例中,每个变量的这个数字都是相同的(所有变量的数字都是5.16),但在我的数据集中却不一样 question mean SE

我有这样的数据。我正在使用调查软件包生成名为vars的向量中每个变量的平均值、SE和FREQ。我是在R中操作列表的新手&非常感谢您的帮助

library(survey)
我想提取/操作上面的列表,以创建一个看起来更像这样的数据帧: 如您所见,sum_svytable是$svytable generated列表中为每个变量生成的频率之和。尽管在我的示例中,每个变量的这个数字都是相同的(所有变量的数字都是5.16),但在我的数据集中却不一样

question    mean     SE      sum_svytable
weight      0.797   0.1177    5.16
married      0.910  0.071     5.16 
我不介意这个结果是如何得出的,我只需要它在df中


这可能吗?

一个选项是循环“myfun”输出的
列表
,然后提取组件“svymean”,创建一个data.frame,从“svytable”元素中添加
sum
s列,
rbind
列表元素,并从行名称中创建“question”列

sum_svytable was derived like this:  

output of myfun function for weight: 
[[1]]$svytable
weight
0.23 0.55  0.6 0.66 0.67  1.1 1.12 
0.46 0.55 0.60 0.66 0.67 1.10 1.12 

I simply summed the frequencies for each response: 
sum_svytable(for weight) = 0.46 +0.55+ 0.60+ 0.66+ 0.67+ 1.10+ 1.12 

out@akrun请看这里,非常感谢!它是基于相同函数的输出。我得到
交互
@akrun,对不起,会修好的!
question    mean     SE      sum_svytable
weight      0.797   0.1177    5.16
married      0.910  0.071     5.16 
sum_svytable was derived like this:  

output of myfun function for weight: 
[[1]]$svytable
weight
0.23 0.55  0.6 0.66 0.67  1.1 1.12 
0.46 0.55 0.60 0.66 0.67 1.10 1.12 

I simply summed the frequencies for each response: 
sum_svytable(for weight) = 0.46 +0.55+ 0.60+ 0.66+ 0.67+ 1.10+ 1.12 
out <- lapply(vars, myfun)
lst1 <- lapply(out, function(x) 
      cbind(setNames(as.data.frame(x$svymean), c("mean", "SE")),
               sum_svytable = sum(x$svytable)))
out1 <- do.call(rbind, lst1)
out1$question <- row.names(out1)
row.names(out1) <- NULL
out1[c('question', 'mean', 'SE', 'sum_svytable')]
#  question      mean        SE sum_svytable
#1   weight 0.7979070 0.1177470         5.16
#2  married 0.9108527 0.0716663         5.16
#3     pens 0.4627193 0.2254907         4.56