将列表传送到purrr :: map并在.x

时间:2019-02-14 11:17:17

标签: r dplyr pipe purrr

通常,我会生成一些小标题列表,然后希望将它们转发到purrr::map调用中。通常,我想向每个小标题添加一个标识符列,然后将它们连接在一起。我正在寻找一种不必生成会污染全局环境的中间变量的方法,只需能够使用seq_along添加id列即可。

加载库:

library(tidyverse)

生成reprex:

reprex_list <- list(Aleena = structure(list(
  name = "Ratts Tyerell", height = 79L,
  mass = 15, hair_color = "none", skin_color = "grey, blue",
  eye_color = "unknown", birth_year = NA_real_, gender = "male",
  homeworld = "Aleen Minor", films = list("The Phantom Menace"),
  vehicles = list(character(0)), starships = list(character(0))
), class = c(
  "tbl_df",
  "tbl", "data.frame"
), row.names = c(NA, -1L)), Besalisk = structure(list(
  name = "Dexter Jettster", height = 198L, mass = 102, hair_color = "none",
  skin_color = "brown", eye_color = "yellow", birth_year = NA_real_,
  gender = "male", homeworld = "Ojom", films = list("Attack of the Clones"),
  vehicles = list(character(0)), starships = list(character(0))
), class = c(
  "tbl_df",
  "tbl", "data.frame"
), row.names = c(NA, -1L)), Cerean = structure(list(
  name = "Ki-Adi-Mundi", height = 198L, mass = 82, hair_color = "white",
  skin_color = "pale", eye_color = "yellow", birth_year = 92,
  gender = "male", homeworld = "Cerea", films = list(c(
    "Attack of the Clones",
    "The Phantom Menace", "Revenge of the Sith"
  )), vehicles = list(
    character(0)
  ), starships = list(character(0))
), class = c(
  "tbl_df",
  "tbl", "data.frame"
), row.names = c(NA, -1L)))

从这里开始,我要做的是在我的全局环境中生成一个中间变量,然后再次启动地图,如下所示:

species_id <- names(reprex_list) # don't want to have to break the pipe and add this to my blobal environment
map(.x = seq_along(reprex_list), .f = ~reprex_list[[.x]] %>%
  dplyr::mutate(species = species_id[[.x]])) %>%
  map(.f = ~ .x %>% mutate_all(as.character)) %>%
  purrr::reduce(full_join) %>%
  type_convert()

愚蠢的是,我想要的是:

reprex_list %>% # Sometimes this is piped in from many previous lines of code so I don't want to have to assign this to a separate variable to be able to carry on.
  map(.x = seq_along(.), .f = ~ .[[.x]] %>% dplyr::mutate(species = names(.)[[.x]])) %>%
  map(.f = ~ .x %>% mutate_all(as.character)) %>%
  purrr::reduce(full_join) %>%
  type_convert()

但是后者不起作用。现在显然额外的麻烦在这里是最小的,但是有时在生成中间列表之前我已经有多行代码,然后必须将它们分配给一个单独的变量。然后再次开始管道传输,我敢肯定可以在一个代码块中完成,但是我还没有找到解决方法。有任何想法吗?谢谢。

1 个答案:

答案 0 :(得分:0)

尝试在指定variable = "VERY_LONG_STRING" if len(variable) < 1000: variable = "TINY_STRING" 参数的情况下使用purrr::map_dfr

.id

或者甚至最好添加物种列而不做其他事情:

reprex_list %>% map_dfr(~mutate_all(., as.character), .id='species')

编辑:

reprex_list %>% map_dfr(identity, .id='species')
根据上面的评论,

实际上是执行此操作的最佳方法