所以我有这个 list :
structure(list(scaf = structure(1L, .Label = "HE638", class = "factor"),
pos = 8L, scaf = structure(1L, .Label = "HE638", class = "factor"),
pos = 8L, scaf = structure(1L, .Label = "HE638", class = "factor"),
pos = 8L), .Names = c("scaf", "pos", "scaf", "pos", "scaf",
"pos"))
我想得到一个 data.frame 以便两列是
scaf
和 pos
这是我得到的:
do.call(rbind, poor.loci)
想要的结果:
scaf pos
HE638 8
HE638 8
HE638 8
最佳答案
以下是三个可供考虑的选项:
选项 1
直接选择所有其他元素并手动创建您的 data.frame
.
setNames(
data.frame(unlist(poor.loci[c(TRUE, FALSE)], use.names = FALSE),
unlist(poor.loci[c(FALSE, TRUE)], use.names = FALSE)),
unique(names(poor.loci)))
# scaf pos
# 1 HE638 8
# 2 HE638 8
# 3 HE638 8
选项 2
转换您的
list
成“长”data.frame
和 reshape
它变成你想要的形式。也可以使用 melt
使用“reshape2”包来完成和 dcast
而不是 stack
和 reshape
.X <- stack(lapply(poor.loci, as.character))
X$ID <- ave(X$values, X$values, FUN = seq_along)
reshape(X, direction = "wide", idvar="ID", timevar="ind")
# ID values.scaf values.pos
# 1 1 HE638 8
# 3 2 HE638 8
# 5 3 HE638 8
选项 3
重新安排您的
list
并转换重新排列的 list
到 data.frame
:A <- unique(names(poor.loci))
data.frame(
setNames(
lapply(A, function(x) unlist(poor.loci[names(poor.loci) %in% x],
use.names = FALSE)), A))
关于R - 使用列表名称列出 data.frame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20291468/