将列表转换为R中的数据框 [英] Converting list to dataframe in R
本文介绍了将列表转换为R中的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有一个列表,说:
So I have a list, say:
L1 <- list(1:10, 5:14, 10:19)
现在,我正在尝试将列表的输出作为数据帧获取,以使我的输出看起来像这样:
Now I am trying to get the output of the list as dataframe such that my output looks:
1. 1 2 3 4 5 6 7 8 9 10
2. 5 6 7 8 9 10 11 12 13 14
3. 10 11 12 13 14 15 16 17 18 19
我正在使用
as.data.frame(L1, row.names = TRUE)
和
list_vect2df(L1)
但是他们都没有提供所需的输出
But none of them are giving the required output
推荐答案
您可以unlist
并使用matrix
,然后转换为data.frame
.在这种情况下,似乎更快.
You can unlist
and use matrix
, then converting to data.frame
. It seems to be faster for this case.
as.data.frame(matrix(unlist(L1),nrow=length(L1),byrow=TRUE)
microbenchmark::microbenchmark(
a= map_dfr(L1, ~as.data.frame(t(.x))),
b= do.call(rbind, lapply(L1, function(x) as.data.frame(t(x)))),
c= as.data.frame(t(as.data.frame(L1))),
d= data.table::transpose(as.data.frame(L1)),
e= as.data.frame(matrix(unlist(L1),nrow=length(L1),byrow=TRUE)),
times = 100,unit = "relative")
# Unit: relative
# expr min lq mean median uq max neval
# a 9.146545 8.548656 8.859087 8.859051 9.449237 7.265274 100
# b 13.879833 11.523000 11.433790 10.924726 10.797251 24.012107 100
# c 12.719835 10.635809 10.442108 10.229913 10.259789 7.020377 100
# d 10.439881 9.143530 9.205734 8.859026 9.176125 6.624454 100
# e 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 100
这篇关于将列表转换为R中的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文