D.sen
D.sen

Reputation: 922

Extracting elements of nested list by contained value using purrr

I have a nested list where each nested list has the same elements but not in the same order and the elements are not named explicitly but do have a name value within the list.

As you can see in the structure the list containing the 'Date' field appears at the second position in the first list and the third position in the second list, so I can't extract at a position.

I would like to extract the lists where name : is Date and keep the value associated with it, using the purrr package.

STRUCTURE

dplyr::glimpse(my_list)
List of 2
 $ :List of 10
  ..$ :List of 2
  .. ..$ name : chr "MIME-Version"
  .. ..$ value: chr "1.0"
  ..$ :List of 2
  .. ..$ name : chr "Date"
  .. ..$ value: chr "Wed, 13 Feb 2019 15:20:40 -0800"
  ..$ :List of 2
  .. ..$ name : chr "References"
  .. ..$ value: chr "<CAE1g-C7AmC3zoJRG_UgdwwkkSiJMuEuDYLU1j4ni0MZJXNrGNQ@mail.gmail.com>"
  ..$ :List of 2
  .. ..$ name : chr "In-Reply-To"
  .. ..$ value: chr "<CAE1g-C7AmC3zoJRG_UgdwwkkSiJMuEuDYLU1j4ni0MZJXNrGNQ@mail.gmail.com>"
  ..$ :List of 2
  .. ..$ name : chr "Message-ID"
  .. ..$ value: chr "<CAPApPh+WZszKg_bjQBPrS8TOvLA23hQkaa9Hocb_cgrQYs2R1w@mail.gmail.com>"
  ..$ :List of 2
  .. ..$ name : chr "Subject"
  .. ..$ value: chr "Re:"
  ..$ :List of 2
  .. ..$ name : chr "From"
  .. ..$ value: chr ""
  ..$ :List of 2
  .. ..$ name : chr "To"
  .. ..$ value: chr ""
  ..$ :List of 2
  .. ..$ name : chr "Cc"
  .. ..$ value: chr ""
  ..$ :List of 2
  .. ..$ name : chr "Content-Type"
  .. ..$ value: chr "multipart/alternative; boundary=\"000000000000f3d8810581cec99d\""
 $ :List of 7
  ..$ :List of 2
  .. ..$ name : chr "MIME-Version"
  .. ..$ value: chr "1.0"
  ..$ :List of 2
  .. ..$ name : chr "Message-ID"
  .. ..$ value: chr "<CAPApPh+THbnCg2e5WmKEwQwHEEjKDHq3V6LkYV9oL88DbHE9Pg@mail.gmail.com>"
  ..$ :List of 2
  .. ..$ name : chr "Date"
  .. ..$ value: chr "Wed, 13 Feb 2019 12:18:32 -0800"
  ..$ :List of 2
  .. ..$ name : chr "Subject"
  .. ..$ value: chr ""
  ..$ :List of 2
  .. ..$ name : chr "From"
  .. ..$ value: chr "Daniel Seneca <[email protected]>"
  ..$ :List of 2
  .. ..$ name : chr "To"
  .. ..$ value: chr "Daniel Seneca <[email protected]>"
  ..$ :List of 2
  .. ..$ name : chr "Content-Type"
  .. ..$ value: chr "multipart/mixed; boundary=\"000000000000f11ad10581cc3e85\""

DATA

my_list <-list(list(list(name = "MIME-Version", value = "1.0"), list(name = "Date", value = "Wed, 13 Feb 2019 15:20:40 -0800"), list(name = "References", value = "<CAE1g-C7AmC3zoJRG_UgdwwkkSiJMuEuDYLU1j4ni0MZJXNrGNQ@mail.gmail.com>"), list(name = "In-Reply-To", value = "<CAE1g-C7AmC3zoJRG_UgdwwkkSiJMuEuDYLU1j4ni0MZJXNrGNQ@mail.gmail.com>"),list(name = "Message-ID", value = "<CAPApPh+WZszKg_bjQBPrS8TOvLA23hQkaa9Hocb_cgrQYs2R1w@mail.gmail.com>"), list(name = "Subject", value = "Re:"), list(name = "From", value = ""),list(name = "To", value = ""),list(name = "Cc", value = ""),list(name = "Content-Type", value = "multipart/alternative; boundary=\"000000000000f3d8810581cec99d\"")),
               list(list(name = "MIME-Version", value = "1.0"), list(name = "Message-ID",value = "<CAPApPh+THbnCg2e5WmKEwQwHEEjKDHq3V6LkYV9oL88DbHE9Pg@mail.gmail.com>"),list(name = "Date",value = "Wed, 13 Feb 2019 12:18:32 -0800"), list(name = "Subject", value = ""), list(name = "From",value = "Daniel Seneca <[email protected]>"), list(name = "To", value = "Daniel Seneca <[email protected]>"),list(name = "Content-Type", value = "multipart/mixed; boundary=\"000000000000f11ad10581cc3e85\"")))

Upvotes: 0

Views: 737

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 270268

The question does not define what the output is supposed to look like so we will assume that it should be a list of lists. Remove the first layer using flatten as shown and then filter its elements using keep.

library(purrr)
my_list %>% 
  flatten %>%
  keep(~ .x$name == "Date")

In base R this could be written:

Filter(function(x) x$name == "Date", do.call("c", my_list))

Upvotes: 1

Related Questions