按组调整列表级别

Reshape list levels by group

提问人:heikeehee 提问时间:10/12/2023 更新时间:10/12/2023 访问量:48

问:

我有一个大列表,其中包含四个结构不同的数据框元素。每个数据框都有一个 ID 变量,可以多次列出。

下面是我的列表的更简单版本。

set.seed(123) 

fruit <- data.frame(
  id = rep(1:10, 3),
  crop = rep(c("apple", "pear", "banana"), each = 10),
  consumed = sample(0:20, 30, replace=TRUE),
  sold = sample(1:10, 30, replace = TRUE))

veg <- data.frame(
  id = rep(1:10, 2),
  crop = rep(c("potatoe", "onion"), each = 10),
  consumed = sample(0:20, 20, replace=TRUE),
  gifted = sample(0:10, 20, replace=TRUE)
)

milk <- data.frame(
  id = 1:10,
  item = "milk",
  consumed = sample(1:6, 10, replace = T),
  produced  = sample(1:20, 10, replace = T),
  processed = sample(0.5:3.5, 10, replace = T),
  sold = sample(0.5:3.5, 10, replace = T))

food <- list(fruit, veg, milk)

food

我想要么按 ID 拆分列表,其中 ID 是第一级,水果、蔬菜和牛奶成为第二级,要么找到另一种按 ID 跨列表元素分组的方法。基本上,我希望在一个地方将一个特定 ID 的所有内容都应用我编写的函数,该函数汇总了该特定 ID 的所有数据。

我尝试了一些地图选项。

R 列表 子集 purrr

评论

1赞 MrFlick 10/12/2023
您能否为此示例输入提供所需的输出,以便可以测试和验证可能的解决方案?
0赞 TarJae 10/12/2023
你想要这个吗?library(dplyr) food %>% bind_rows() %>% group_split(id)

答:

0赞 ThomasIsCoding 10/12/2023 #1

可能你是在追求这个?

lst <- unlist(lapply(food, split, ~id), recursive = FALSE)

split(lst, names(lst))

这给了

$`1`
$`1`$`1`
   id   crop consumed sold
1   1  apple       14    9
11  1   pear        4    5
21  1 banana       13    4

$`1`$`1`
   id    crop consumed gifted
1   1 potatoe        1      2
11  1   onion       11      2

$`1`$`1`
  id item consumed produced processed sold
1  1 milk        6       14       2.5  3.5


$`10`
$`10`$`10`
   id   crop consumed sold
10 10  apple       13    2
20 10   pear        3   10
30 10 banana        6    1

$`10`$`10`
   id    crop consumed gifted
10 10 potatoe       15      5
20 10   onion        7      9

$`10`$`10`
   id item consumed produced processed sold
10 10 milk        4       20       3.5  3.5


$`2`
$`2`$`2`
   id   crop consumed sold
2   2  apple       18    9
12  2   pear       18    8
22  2 banana       16    6

$`2`$`2`
   id    crop consumed gifted
2   2 potatoe        3      9
12  2   onion       13      7

$`2`$`2`
  id item consumed produced processed sold
2  2 milk        6       19       2.5  1.5


$`3`
$`3`$`3`
   id   crop consumed sold
3   3  apple       13   10
13  3   pear        8    2
23  3 banana       10    8

$`3`$`3`
   id    crop consumed gifted
3   3 potatoe       12      1
13  3   onion        2      2

$`3`$`3`
  id item consumed produced processed sold
3  3 milk        3        7       3.5  2.5


$`4`
$`4`$`4`
   id   crop consumed sold
4   4  apple        2    7
14  4   pear        2    1
24  4 banana        6    6

$`4`$`4`
   id    crop consumed gifted
4   4 potatoe        4      9
14  4   onion       13      7

$`4`$`4`
  id item consumed produced processed sold
4  4 milk        2        9       0.5  3.5


$`5`
$`5`$`5`
   id   crop consumed sold
5   5  apple        9    5
15  5   pear        7    9
25  5 banana       20    6

$`5`$`5`
   id    crop consumed gifted
5   5 potatoe       18      1
15  5   onion        6      0

$`5`$`5`
  id item consumed produced processed sold
5  5 milk        5        7       3.5  3.5


$`6`
$`6`$`6`
   id   crop consumed sold
6   6  apple       17    7
16  6   pear        6    9
26  6 banana       11    7

$`6`$`6`
   id    crop consumed gifted
6   6 potatoe       19      9
16  6   onion        2      6

$`6`$`6`
  id item consumed produced processed sold
6  6 milk        6        2       1.5  3.5


$`7`
$`7`$`7`
   id   crop consumed sold
7   7  apple       10    5
17  7   pear        9    6
27  7 banana       14    1

$`7`$`7`
   id    crop consumed gifted
7   7 potatoe       13      5
17  7   onion       14     10

$`7`$`7`
  id item consumed produced processed sold
7  7 milk        5       16       1.5  2.5


$`8`
$`8`$`8`
   id   crop consumed sold
8   8  apple        4    6
18  8   pear        8    5
28  8 banana        9    6

$`8`$`8`
   id    crop consumed gifted
8   8 potatoe        2      3
18  8   onion       20      6

$`8`$`8`
  id item consumed produced processed sold
8  8 milk        3       13       3.5  0.5


$`9`
$`9`$`9`
   id   crop consumed sold
9   9  apple       19    9
19  9   pear       18    9
29  9 banana       12    2

$`9`$`9`
   id    crop consumed gifted
9   9 potatoe        7      0
19  9   onion        4      6

$`9`$`9`
  id item consumed produced processed sold
9  9 milk        4       19       0.5  3.5
1赞 langtang 10/12/2023 #2

下面是一个选项,它使用 然后创建一个数据框,该数据框每个数据框都有一行,并且 、 和 帧嵌套在列中:tidyr::nest()purrr::reduce()idmilkfruitveg

names(food) <- c("fruit", "veg", "milk")

lapply(names(food), \(f) nest(food[[f]], .by=id,.key =f )) %>%
  reduce(full_join, by="id")

输出:

      id fruit            veg              milk            
   <int> <list>           <list>           <list>          
 1     1 <tibble [3 × 3]> <tibble [2 × 3]> <tibble [1 × 5]>
 2     2 <tibble [3 × 3]> <tibble [2 × 3]> <tibble [1 × 5]>
 3     3 <tibble [3 × 3]> <tibble [2 × 3]> <tibble [1 × 5]>
 4     4 <tibble [3 × 3]> <tibble [2 × 3]> <tibble [1 × 5]>
 5     5 <tibble [3 × 3]> <tibble [2 × 3]> <tibble [1 × 5]>
 6     6 <tibble [3 × 3]> <tibble [2 × 3]> <tibble [1 × 5]>
 7     7 <tibble [3 × 3]> <tibble [2 × 3]> <tibble [1 × 5]>
 8     8 <tibble [3 × 3]> <tibble [2 × 3]> <tibble [1 × 5]>
 9     9 <tibble [3 × 3]> <tibble [2 × 3]> <tibble [1 × 5]>
10    10 <tibble [3 × 3]> <tibble [2 × 3]> <tibble [1 × 5]>

评论

0赞 heikeehee 10/13/2023
这非常有帮助,可能是我正在寻找的。但是,如何从嵌套数据框转到嵌套列表?所以ID是第一级,食物嵌套为列表?$ $$ $$ $$ $$ $$ $$ $$ 等。11fruit1veg1milk22fruit2veg2milk