[英]Is there a way in R to create summary statistics table from a list of data
I am trying to make a summary table of my data using table1::table1 function.我正在尝试使用 table1::table1 function 制作我的数据汇总表。
I can get the below code to work as expected.我可以让下面的代码按预期工作。
DF = list('A' = rnorm(100, mean = 1),
'B' = rnorm(100, mean = 2),
'C' = rnorm(100, mean = 3))
table1::table1(~A+B+C, data=DF, overall = 'List 1')
I am trying to do the same thing as above but instead, I have a list of data that I want to combine and have the columns as List 1 and List 2, with the same summary statistics from A, B, C as the rows.我正在尝试做与上面相同的事情,但相反,我有一个我想要组合的数据列表,并将列作为列表 1 和列表 2,与来自 A、B、C 的相同汇总统计数据作为行。
DF = NULL
DF[['List 1']] = list('A' = rnorm(100, mean = 1),
'B' = rnorm(100, mean = 2),
'C' = rnorm(100, mean = 3))
DF[['List 2']] = list('A' = rnorm(100, mean = 1),
'B' = rnorm(100, mean = 2),
'C' = rnorm(100, mean = 3))
Does anyone have any suggestions or know of a better way to accomplish this?有没有人有任何建议或知道更好的方法来实现这一点?
We can use transpose
from purrr
and flatten
or unlist
the list
elements to be a list
of 3我们可以使用purrr
中的transpose
并将list
元素flatten
或unlist
列出为 3 个list
library(purrr)
DF2 <- transpose(DF) %>%
map(flatten_dbl)
NOTE: DF
is the second dataset in the OP's post注意: DF
是 OP 帖子中的第二个数据集
Now, we test with table1
现在,我们用table1
进行测试
table1::table1(~A+B+C, data=DF2, overall = 'List 1')
-output -输出
If we need to create a two column table, convert the list
of list
of vectors to a single data.frame
while creating a column with the names
of the outer list
element如果我们需要创建一个两列表,请将向量list
list
转换为单个data.frame
,同时使用外部list
元素的names
创建列
new_DF <- do.call(rbind, Map(cbind, nm1 = names(DF),
lapply(DF, as.data.frame)))
row.names(new_DF) <- NULL
table1::table1(~ A + B + C |nm1, data = new_DF)
-output -输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.