If I have the following list:
mylist <- list(
data.frame(x = c("One", "Two", "Three", "Four"), y = c(1,2,3,4)), # two strings between one and four
data.frame(x = c("Three", "One", "Six", "Four"), y = c(4,5,6,7)), # one string between one and four
data.frame(x = c("Six", "Seven", "Eight"), y = c(7,8,9)), # no strings between one and four
data.frame(x = c("Six", "One", "Eight"), y = c(10,11,12)), # no four
data.frame(x = c("Six", "Four", "Eight"), y = c(13,14,15)) # no one
)
How would I use purrr [I've tried using keep but couldn't figure it out] to return each list's x values between One and Four -- I also may have cases where there is One without Four and Four without One.
Desired Output
[[1]]
One Two Three Four
[[2]]
One Six Four
[[3]]
NULL
[[4]]
One
[[5]]
Four
Is this possible?
Another example:
mylist <- list(
data.frame(x = c("Bob", "Sarah", "Sally", "Mark")), # two strings between Bob and Mark
data.frame(x = c("Teegan", "Bob", "Rachel", "Mark")), # one string between Bob and Mark
data.frame(x = c("Tara", "Gus", "Melissa")), # neither Bob nor Mark
data.frame(x = c("Sam", "Matt", "Bob")), # no mark
data.frame(x = c("Mark", "Jordan", "Maya")),
data.frame(x = c("Mark", "Bob", "Rachel")) #dont return names after Mark
)
Returns:
[[1]] Bob Sarah Sally Mark
[[2]] Bob Rachel Mark
[[3]] NULL
[[4]] Bob
[[5]] Mark
[[6]] Bob Mark