问题描述
我从分析中获得以下命名列表输出.可复制的代码如下:
I have the following named list output from a analysis. The reproducible code is as follows:
list(structure(c(-213.555409754509, -212.033637890131, -212.029474755074,
-211.320398316741, -211.158815833294, -210.470525157849), .Names = c("wasn",
"chappal", "mummyji", "kmph", "flung", "movie")), structure(c(-220.119433774144,
-219.186901747536, -218.743319709963, -218.088361753899, -217.338920075687,
-217.186050877079), .Names = c("crazy", "wired", "skanndtyagi",
"andr", "unveiled", "contraption")))
我想将其转换为数据帧.我尝试使用reshape2,dplyr和其他用于将列表转换为数据帧的解决方案来取消列表到数据帧的选项,但没有成功.我正在寻找的输出是这样的:
I want to convert this to a data frame. I have tried unlist to data frame options using reshape2, dplyr and other solutions given for converting a list to a data frame but without much success. The output that I am looking for is something like this:
Col1 Val1 Col2 Val2
1 wasn -213.55 crazy -220.11
2 chappal -212.03 wired -219.18
3 mummyji -212.02 skanndtyagi -218.74
等等.实际的输出有多列具有成对值的列,并且有很多行.我已经尝试过以下代码:
so on and so forth. The actual out put has multiple columns with paired values and runs into many rows. I have tried the following codes already:
do.call(rbind, lapply(df, data.frame, stringsAsFactors = TRUE))
部分在列中提供所有字符值,在第二栏中提供数字值.
works partially provides all the character values in a column and numeric values in the second.
data.frame(Reduce(rbind, df))
行不通-在第一个列表中提供名称,并在两个列表中提供数字作为两个不同的行
didn't work - provides the names in the first list and numbers from both the lists as tow different rows
colNames <- unique(unlist(lapply(df, names)))
M <- matrix(0, nrow = length(df), ncol = length(colNames),
dimnames = list(names(df), colNames))
matches <- lapply(df, function(x) match(names(x), colNames))
M[cbind(rep(sequence(nrow(M)), sapply(matches, length)),
unlist(matches))] <- unlist(df)
M
无法正常工作.
有人可以帮忙吗?
推荐答案
由于列表元素的长度都相同,因此您应该能够stack
将它们按列合并.
Since the list elements are all of the same length, you should be able to stack
them and then combine them by columns.
尝试:
do.call(cbind, lapply(myList, stack))
这篇关于嵌套命名列表到数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!