【问题标题】:How do I convert a tribble to a list and convert function names?如何将 tribble 转换为列表并转换函数名称?
【发布时间】:2020-02-27 21:58:32
【问题描述】:

我有一个我创建的示例tbl_df。我正在寻找为每个列表名称使用list_name 列的列表,并填写列表以匹配我下面的example_list。此外,我想将scrape_func 列中的字符值转换为将实际函数的名称存储在列表中。我仅以列表中的mean 为例。

示例tbl_df

example_df <-tibble::tribble(
   ~list_name, ~abbr,        ~id, ~scrape_func,
      "pepsi", "pep", "pepsi_id",       "mean",
       "coke",  "ck",  "coke_id",       "mean",
  "dr_pepper", "drp",    "dr_id",       "mean"
  )

我要查找的列表格式:

example_list <- list(
  "pepsi" = list(
    list_name = "pepsi",
    abbr = "pep",
    id = "pepsi_id",
    scrape_func = mean
  ),
  "coke" = list(
    list_name = "coke",
    abbr = "ck",
    id = "coke_id",
    scrape_func = mean
  ),
  "dr_pepper" = list(
    list_name = "dr_pepper",
    abbr = "drp",
    id = "dr_id",
    scrape_func = mean
  )
)

如果可能,我希望在tidyverse 中使用解决方案。提前致谢!

【问题讨论】:

    标签: r list dplyr purrr


    【解决方案1】:

    我们可以使用get获取函数的值,使用pmap可以循环遍历list

    library(dplyr)
    library(purrr)
    out <- example_df %>% 
             mutate(scrape_func = map(scrape_func, get)) %>%
             pmap(c) %>% 
             set_names(example_df$list_name)
    

    检查预期输出

    all.equal(out, example_list)
    #[1] TRUE
    

    或者match.fun

    out <- example_df %>% 
             mutate(scrape_func = map(scrape_func, match.fun)) %>%
              pmap(c) %>% 
              set_names(example_df$list_name)
    all.equal(out, example_list)
    #[1] TRUE
    

    更新

    如果 OP 想要存储为符号,请使用 as.symbolas.namerlang::symrlang::syms

    out2 <-  example_df %>%
                mutate(scrape_func = map(scrape_func, as.name)) %>%
                pmap(c) %>% 
                set_names(example_df$list_name)
    
    out2$pepsi$scrape_func
    #mean
    

    我们可以通过match.fun获取值

    match.fun(out2$pepsi$scrape_func)
    #function (x, ...) 
    #UseMethod("mean")
    #<bytecode: 0x7ffdc09fef58>
    #<environment: namespace:base>
    

    【讨论】:

    • 这正是我想要的。谢谢你。使用 get 或 match.fun 有什么好处吗?还有,pmap(c) 有什么作用?
    • @Jazzmatazz pmap c 将每一行的list 列连接成一个vectormatch.funget 更安全
    • 最后一个问题。如何打印out 以显示类似于example_list 的列表格式?我想将列表存储在我的包中。
    • @Jazzmatazz 你的意思是dput(example_list) ?
    • 我认为应该这样做。请保留这两个解决方案,因为我可能会有两个解决方案的用例。谢谢!
    【解决方案2】:

    也许是这样的?

    setNames(apply(example_df, 1, as.list), example_df$list_name)
    #> $pepsi
    #> $pepsi$list_name
    #> [1] "pepsi"
    #> 
    #> $pepsi$abbr
    #> [1] "pep"
    #> 
    #> $pepsi$id
    #> [1] "pepsi_id"
    #> 
    #> $pepsi$scrape_func
    #> [1] "mean"
    #> 
    #> 
    #> $coke
    #> $coke$list_name
    #> [1] "coke"
    #> 
    #> $coke$abbr
    #> [1] "ck"
    #> 
    #> $coke$id
    #> [1] "coke_id"
    #> 
    #> $coke$scrape_func
    #> [1] "mean"
    #> 
    #> 
    #> $dr_pepper
    #> $dr_pepper$list_name
    #> [1] "dr_pepper"
    #> 
    #> $dr_pepper$abbr
    #> [1] "drp"
    #> 
    #> $dr_pepper$id
    #> [1] "dr_id"
    #> 
    #> $dr_pepper$scrape_func
    #> [1] "mean"
    

    reprex package (v0.3.0) 于 2020 年 2 月 27 日创建

    【讨论】:

    • 谢谢!欣赏回应。这不包括列表中的实际功能。 akrun 的解决方案奏效了。
    猜你喜欢
    • 1970-01-01
    • 2020-07-06
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多