Seymoo
Seymoo

Reputation: 189

removing sublists from a list

I have list of 155 elements, eahc contain 3 lists.

below I made an small example. I am only interested in keeping values in gene and am trying in R to remove first and second list of each element all at once! leaving me only values in gene.

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)

How to achieve that?

Upvotes: 2

Views: 1614

Answers (1)

Mike H.
Mike H.

Reputation: 14360

As shown by the dput you posted in the comments, your actual data structure is a list of lists. In this case, you can use an lapply to get what you want:

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"  

Upvotes: 2

Related Questions