取消嵌套列表并保留名称

迈克·布斯

我怀疑这是一个微不足道的问题,但我无法弄清楚。我从生成此列表数据的打包函数中输出:

output <- list(structure(c(69, 1.52224832314379, 5.1, 0.362256088534843, 
46.9, -0.0364138250590129, 90.7, 3.0809104713466), .Dim = c(2L, 
4L), .Dimnames = list(structure(c("N", "k"), .Dim = 1:2), c("Estimate", 
"SE", "95% LCI", "95% UCI"))))

我想将其转换为带有列c(“ Parameter”,“ Estimate”,“ SE”,“ 95%LCI”,“ 95%UCI”)的数据帧,其中Parameter = c(“ N”,“ k”)

我尝试了dplyr :: unnest(output)no applicable method for 'unnest' applied to an object of class "list",unlist(output),它返回c(69, 1.52224832314379, 5.1, 0.362256088534843, 46.9, -0.0364138250590129, 90.7, 3.0809104713466)但不保留任何名称。purrr :: flatten(output)也不保留任何名称。

顺便说一句,我也想不出如何将名称从列表中拉出-dimnames()和names()返回NULL。

davidnortes

如果您查看对象的类

> class(output)
[1] "list"

> class(output[[1]])
[1] "matrix"

您可以简单地执行以下操作:

library(magrittr)
df <- as.data.frame(output) %>% tibble::rownames_to_column("Parameter")

  Parameter  Estimate        SE    X95..LCI X95..UCI
1         N 69.000000 5.1000000 46.90000000 90.70000
2         k  1.522248 0.3622561 -0.03641383  3.08091

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章