以下三个选项需要考虑:
选项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
其转换为您想要的形式.也可以使用的"reshape2"包做melt
和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))