使用 group_split,将单个值添加到列表中的每个项目以循环和累加
Using group_split, add a single value to each item in a list for looping and accumulating over
我有一个使用 dplyr 的 group_split 函数的分组数据框,例如
mylist <- diamonds %>% group_by(cut, color) %>% group_split
列表项按顺序排列。我的目标是最终映射每个项目和 'build up' 或累积一个值,例如每个的最低价格。
例如,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,还包含另一个项目,即该组的最低价格?
万一有 'better' 方法来做我最终想做的事情,我计划对我的列表做的是 purrr::map
对每个项目并应用一个函数,该函数同时接受列表项目 tbl 和该组的单个数值 min(price),然后通过循环传递最低价格,在每次迭代时将其添加 nrow(tbl) 次。
根据我的目标构建我的列表的最佳方式是什么?
我们可以使用 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
或许,我们可以通过遍历 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
我有一个使用 dplyr 的 group_split 函数的分组数据框,例如
mylist <- diamonds %>% group_by(cut, color) %>% group_split
列表项按顺序排列。我的目标是最终映射每个项目和 'build up' 或累积一个值,例如每个的最低价格。
例如,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,还包含另一个项目,即该组的最低价格?
万一有 'better' 方法来做我最终想做的事情,我计划对我的列表做的是 purrr::map
对每个项目并应用一个函数,该函数同时接受列表项目 tbl 和该组的单个数值 min(price),然后通过循环传递最低价格,在每次迭代时将其添加 nrow(tbl) 次。
根据我的目标构建我的列表的最佳方式是什么?
我们可以使用 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
或许,我们可以通过遍历 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