我有155个元素的列表,eahc包含3个列表。
下面我做了一个小例子。我只想保留gene
中的值,并尝试在R
中同时删除每个元素的第一个和第二个列表!只留下基因中的价值观。
test <- list(name="Adipose", desc= "Roche", gene = c("KRT14", "RPE65"))
test1 <- list(name="muscle", desc= "Roche", gene = c("THRSP", "KRT14"))
test2 <- list(name="WBC" , desc= "Roche", gene = c("RBP4", "CCDC80"))
x <- c(test,test1, test2)
如何实现?
答案 0 :(得分:1)
如您在评论中发布的dput
所示,您的实际数据结构是列表列表。在这种情况下,您可以使用lapply
来获得所需内容:
list <- structure(list(Adipose = structure(list(name = "Adipose", desc = "Roche", genes = c("ACACB", "ACP5", "ACTA1")), .Names = c("name", "desc", "genes")), WBC = structure(list( name = "WBC ", desc = "Roche", genes = c("THRSP", "KRT14", "APOB", "LEP")), .Names = c("name", "desc", "genes"))), .Names = c("Adipose ", "WBC "))
lapply(list, function(x) x[names(x)=="genes"])
#$`Adipose `
#$`Adipose `$genes
#[1] "ACACB" "ACP5" "ACTA1"
#
#$`WBC `
#$`WBC `$genes
#[1] "THRSP" "KRT14" "APOB" "LEP"