【问题标题】:Using group_split, add a single value to each item in a list for looping and accumulating over使用 group_split,将单个值添加到列表中的每个项目,以便循环和累积
【发布时间】:2021-07-07 18:01:02
【问题描述】:

我有一个使用 dplyr 的 group_split 函数的分组数据框,例如

mylist <- diamonds %>% group_by(cut, color) %>% group_split

列表项是按顺序排列的数字。我的目标是最终映射每个项目并“建立”或积累一个价值,例如每个的最低价格。

例如,mylist 中的第一项是:

mylist[1]
<list_of<
  tbl_df<
    carat  : double
    cut    : ordered<90576>
    color  : ordered<bd2ad>
    clarity: ordered<ecdea>
    depth  : double
    table  : double
    price  : integer
    x      : double
    y      : double
    z      : double
  >
>[1]>
[[1]]
# A tibble: 163 x 10
   carat cut   color clarity depth table price     x     y     z
   <dbl> <ord> <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
 1  0.75 Fair  D     SI2      64.6    57  2848  5.74  5.72  3.7 
 2  0.71 Fair  D     VS2      56.9    65  2858  5.89  5.84  3.34
 3  0.9  Fair  D     SI2      66.9    57  2885  6.02  5.9   3.99
 4  1    Fair  D     SI2      69.3    58  2974  5.96  5.87  4.1 
 5  1.01 Fair  D     SI2      64.6    56  3003  6.31  6.24  4.05
 6  0.73 Fair  D     VS1      66      54  3047  5.56  5.66  3.7 
 7  0.71 Fair  D     VS2      64.7    58  3077  5.61  5.58  3.62
 8  0.91 Fair  D     SI2      62.5    66  3079  6.08  6.01  3.78
 9  0.9  Fair  D     SI2      65.9    59  3205  6     5.95  3.94
10  0.9  Fair  D     SI2      66      58  3205  6     5.97  3.95
# … with 153 more rows

假设我想在这个列表项旁边添加一个变量,它是分组的最低价格,有没有办法让mylist[1] 不仅包含一个 tbl,还包含另一个项目,它是最低价格这个组?

如果有一种“更好”的方式来做我最终想做的事情,我计划对我的列表做的事情是 purrr::map 在每个项目上并应用一个函数,该函数同时接受列表项 tbl 和单个数字该组的值 min(price),然后将 min price 通过循环传递,在每次迭代 nrow(tbl) 次时添加到它。

考虑到我的目标,构建 mylist 的最佳方式是什么?

【问题讨论】:

    标签: r dplyr purrr


    【解决方案1】:

    也许,我们可以通过循环 list 来创建一个名为 list

    library(purrr)
    out <- map(mylist, ~ list(data = ., min_price = min(.$price)))
    

    -检查

    > out[[1]]$data
    # A tibble: 163 x 10
       carat cut   color clarity depth table price     x     y     z
       <dbl> <ord> <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
     1  0.75 Fair  D     SI2      64.6    57  2848  5.74  5.72  3.7 
     2  0.71 Fair  D     VS2      56.9    65  2858  5.89  5.84  3.34
     3  0.9  Fair  D     SI2      66.9    57  2885  6.02  5.9   3.99
     4  1    Fair  D     SI2      69.3    58  2974  5.96  5.87  4.1 
     5  1.01 Fair  D     SI2      64.6    56  3003  6.31  6.24  4.05
     6  0.73 Fair  D     VS1      66      54  3047  5.56  5.66  3.7 
     7  0.71 Fair  D     VS2      64.7    58  3077  5.61  5.58  3.62
     8  0.91 Fair  D     SI2      62.5    66  3079  6.08  6.01  3.78
     9  0.9  Fair  D     SI2      65.9    59  3205  6     5.95  3.94
    10  0.9  Fair  D     SI2      66      58  3205  6     5.97  3.95
    # … with 153 more rows
    > out[[1]]$min_price
    [1] 536
    

    【讨论】:

      【解决方案2】:

      我们可以使用imap为我们的输出变量创建一个更具体的名称:

      mylist %>%
        imap(~ .x %>%
               mutate(!!paste("Min_Price", .y) := reduce(price, min))) %>%
        magrittr::extract(1)
      
      [[1]]
      # A tibble: 163 x 11
         carat cut   color clarity depth table price     x     y     z `Min_Price 1`
         <dbl> <ord> <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>         <int>
       1  0.75 Fair  D     SI2      64.6    57  2848  5.74  5.72  3.7            536
       2  0.71 Fair  D     VS2      56.9    65  2858  5.89  5.84  3.34           536
       3  0.9  Fair  D     SI2      66.9    57  2885  6.02  5.9   3.99           536
       4  1    Fair  D     SI2      69.3    58  2974  5.96  5.87  4.1            536
       5  1.01 Fair  D     SI2      64.6    56  3003  6.31  6.24  4.05           536
       6  0.73 Fair  D     VS1      66      54  3047  5.56  5.66  3.7            536
       7  0.71 Fair  D     VS2      64.7    58  3077  5.61  5.58  3.62           536
       8  0.91 Fair  D     SI2      62.5    66  3079  6.08  6.01  3.78           536
       9  0.9  Fair  D     SI2      65.9    59  3205  6     5.95  3.94           536
      10  0.9  Fair  D     SI2      66      58  3205  6     5.97  3.95           536
      # ... with 153 more rows
      

      【讨论】:

      • 感谢您的回答,我在清楚地传达我的问题方面做得很差,对此感到抱歉。接受了 Arun 的回答,这正是我所需要的。一样的感谢!
      • 不,没关系,Arun 是我们中最好的 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 2021-12-22
      • 1970-01-01
      • 2021-10-31
      • 2023-01-18
      • 1970-01-01
      相关资源
      最近更新 更多