代码之家  ›  专栏  ›  技术社区  ›  Doug Fir

映射列表,获取项目名称和内容

  •  1
  • Doug Fir  · 技术社区  · 3 年前
    my_dfs <- list(
      mtcars = mtcars,
      diamonds = diamonds
    )
    map(.x = my_dfs, ~ print(.x %>% str)) # OK
    

    map(.x = my_dfs, ~ print(.id)) # found a reference to .id in the docs.
    Error in print(.id) : object '.id' not found
    

    更好的是,我希望两者都能在一次呼叫地图,例如。

    map(.x = my_dfs, ~ print('About to print str of ' [somehow get list item name here] .x %>% str))
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   Adam    3 年前

    使用 imap() . 内容将由 .x .y .

    z <- list(x = "whats in x",
              y = "whats in y")
    
    imap(z, ~ paste(.y, "has contents:", .x))
    # $x
    # [1] "x has contents: whats in x"
    # 
    # $y
    # [1] "y has contents: whats in y"