I have searched SO but could not find relevant answer.
Sample data:
example.list <- list(list("a",list("b"),list("c")),list("c"))
I would like to subset list that contains letter "c" :
Here this gets the last index node which contains letter "c":
check.a <- lapply(example.list, function(x) grep("c",x))
How I'm going to get the list with the above index? Or how to otherwise get the list of list?
A part of getting the last nodes of list c(1,3) there are preceding list nodes c(1,2).
EDIT: Desired output: (something like this)
[[1]][[3]]
[[1]][[3]][[1]]
[1] "c"
[[2]]
[[2]][[1]]
[1] "c"
However what I need is also to understand with that provided indexing, how can I get the subset of nested list? Please use check.a
.
EDIT 2:
So here are the list indexes that could be used to subset the relevant list nodes:
first.list.index <- which(lapply(lapply(example.list, function(x) grep("c",x)),length)>0)
last.list.index <- unlist(lapply(example.list, function(x) grep("c",x)))
The idea is this: (not working, just to demonstrate what I'm after)
lapply(list(a=first.list.index,b=last.list.index), function(a,b) example.list[[a]][[b]])
EDIT 3: (LAST EDIT)
The actual data looks like this: (I hoped for solution with the indexing I provided, this is why I reduced the question on that)
[[1]]
[[1]][[1]]
[1] "a"
[[1]][[1]]$data
[[1]][[2]]
[[1]][[2]][[1]]
[1] "b"
[[1]][[1]]$data
[[1]][[3]]
[[1]][[3]][[1]]
[1] "c"
[[1]][[1]]$data
[[2]]
[[2]][[1]]
[1] "c"
[[2]][[1]]$data
Sorry for this mess!
Here is reduced dput:
list(list(structure(list(name = "a", data = c("21016954")), .Names = c("name", "data"
)), structure(list(name = "b", data = c("17103795")), .Names = c("name", "data")), structure(list(name = "c",
data = c("38036543")), .Names = c("name", "data"))), list(structure(list(name = "c", data = c("42456597")), .Names = c("name","data"))))
To subset lists of lists, you can use the same technique as before: square brackets.
Definition: A list of lists in Python is a list object where each list element is a list by itself. Create a list of list in Python by using the square bracket notation to create a nested list [[1, 2, 3], [4, 5, 6], [7, 8, 9]] .
To subset lists we can utilize the single bracket [ ] , double brackets [[ ]] , and dollar sign $ operators. Each approach provides a specific purpose and can be combined in different ways to achieve the following subsetting objectives: Subset list and preserve output as a list.
The "find" method for lists is index . I do consider the inconsistency between string. find and list. index to be unfortunate, both in name and behavior: string.
Your second edit is along the right lines, but you need to use Map
instead of lapply
. Using your data
d <- list(list(structure(list(name = "a", data = c("21016954")), .Names = c("name", "data"
)), structure(list(name = "b", data = c("17103795")), .Names = c("name", "data")), structure(list(name = "c",
data = c("38036543")), .Names = c("name", "data"))), list(structure(list(name = "c", data = c("42456597")), .Names = c("name","data"))))
Map(function(i, j) d[[i]][[j]],
i = which(lapply(lapply(d, function(x) grep("c",x)),length)>0),
j = unlist(lapply(d, function(x) grep("c",x))))
#[[1]]
#[[1]]$name
#[1] "c"
#
#[[1]]$data
#[1] "38036543"
#
#
#[[2]]
#[[2]]$name
#[1] "c"
#
#[[2]]$data
#[1] "42456597"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With