2

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?

Seymoo
  • 177
  • 2
  • 15
  • 3
    `x[grepl("gene",names(x))]`? You can't simply do `x["gene"]` since it will only pull the first one. It would be more helpful if you could clarify your data structure. From your description it seems to me `x` is a list of lists, but in your output `x` is simply a list – Mike H. Oct 05 '17 at 13:03
  • Possible duplicate of [How to deal with the possibility of multiple list elements having the same names](https://stackoverflow.com/questions/13808558/how-to-deal-with-the-possibility-of-multiple-list-elements-having-the-same-names) – Jan Oct 05 '17 at 13:05
  • 2
    @MikeH. No need for the `which` and `grepl`: `x[names(x) == 'gene']` does the job. – Paul Hiemstra Oct 05 '17 at 13:06
  • @PaulHiemstra yup, should've thought about that. Was too quick to put down an answer. Don't even need the `which`, could've just done the `grepl`... – Mike H. Oct 05 '17 at 13:07
  • 2
    If it is always in the order, `x[c(FALSE, FALSE, TRUE)]` – akrun Oct 05 '17 at 13:08
  • 1
    @MikeH. If you want to match `gene` 1:1, the `grepl` isn't needed. – Paul Hiemstra Oct 05 '17 at 13:09
  • Yea, it's definitely not the best answer. Also I think we need more clarity from OP on what exactly the structure of his list is. – Mike H. Oct 05 '17 at 13:12
  • Can you paste a `dput(list[1:3])`? – Mike H. Oct 05 '17 at 13:30
  • 1
    @MikeH solution works! however it is not reproducible in the my data, because the structure of real list is different and I don't now how to make it. It's like `str(list) List of 155 $ Adipose :List of 3 ..$ name : chr "Adipose" ..$ desc : chr "Roche" ..$ genes: chr [1:82] "ACACB" "ACP5" "ACTA1" "ADRB3" ... $ WBC :List of 3 ..$ name : chr "WBC" ..$ desc : chr "Roche" ..$ genes: chr [1:28] "THRSP" "KRT14" "APOB" "LEP" ...` I wrote this `for ( i in list[[1:155]]) as.list(name=list[[i]][1], gene=list[[i]][3] )` Didn't work! – Seymoo Oct 05 '17 at 13:30
  • @symo also, I suspect that this will work: `lapply(xx, function(x) x[names(x)=="gene"])` given your above example. However, it would be helpful if you post a `dput()` – Mike H. Oct 05 '17 at 13:33
  • `dput(list[1:2]) 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 "))` – Seymoo Oct 05 '17 at 13:43

1 Answers1

2

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"  
Mike H.
  • 13,960
  • 2
  • 29
  • 39
  • Thanks a lot it works fine. before I make another question do you know a way to find intersects within each `$genes` in `list` ? – Seymoo Oct 05 '17 at 14:05
  • Do you want an intersection across all of the elements in the list? – Mike H. Oct 05 '17 at 14:29
  • just the elements in list $genes in one go, for example `Adipose$genes, WBC$genes` – Seymoo Oct 05 '17 at 15:26
  • This might help https://stackoverflow.com/questions/3695677/how-to-find-common-elements-from-multiple-vectors. Try using the `Reduce` solution in the first answer. If it doesn't i'd recommend posting a new question. – Mike H. Oct 05 '17 at 16:06