我正在尝试根据R中的嵌套列表创建一个数据框。这是一个示例:
mylist<-list(file1 = list("a", sample1 = list(x = 2, y = list(c(1, 2)),
sample2 = list(x = 4, y = list(c(3, 8))))), file2 = list(
"a", sample1 = list(x = 6, y = list(c(6, 4)), sample2 = list(
x = 6, y = list(c(7, 4))))))
我想知道如何从嵌套列表中提取所有特征“ x”和特征“ y”,并将“ y”分为两列;每个值一个?
谢谢大家的宝贵时间!
答案 0 :(得分:1)
我不确定您预期的输出应该是什么样的,但是也许是这样的?
library(tidyverse)
unlist(mylist) %>%
data.frame(val = .) %>%
rownames_to_column("id") %>%
filter(str_detect(id, "(x|y1|y2)")) %>%
separate(id, into = c("id", "col"), sep = "\\.(?=\\w+$)") %>%
spread(col, val)
# id x y1 y2
#1 file1.sample1 2 1 2
#2 file1.sample1.sample2 4 3 8
#3 file2.sample1 6 6 4
#4 file2.sample1.sample2 6 7 4