使用purrr

时间:2019-02-16 22:38:51

标签: r purrr

我有一个嵌套列表,其中每个嵌套列表具有相同的元素,但顺序不同,并且这些元素未明确命名,但在列表中确实具有名称值。

正如您在结构中看到的那样,包含“日期”字段的列表出现在第一个列表的第二个位置,第二个列表中的第三个位置,所以我无法在某个位置提取。

我想使用name :包提取Datepurrr的列表,并保持与之关联的值。

结构

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 <senecad@gene.com>"
  ..$ :List of 2
  .. ..$ name : chr "To"
  .. ..$ value: chr "Daniel Seneca <seneca.daniel@gene.com>"
  ..$ :List of 2
  .. ..$ name : chr "Content-Type"
  .. ..$ value: chr "multipart/mixed; boundary=\"000000000000f11ad10581cc3e85\""

数据

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 <senecad@gene.com>"), list(name = "To", value = "Daniel Seneca <seneca.daniel@gene.com>"),list(name = "Content-Type", value = "multipart/mixed; boundary=\"000000000000f11ad10581cc3e85\"")))

1 个答案:

答案 0 :(得分:1)

该问题并未定义输出的外观,因此我们假定其应为列表列表。如图所示,使用flatten删除第一层,然后使用keep过滤其元素。

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

在基数R中可以这样写:

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