从 1:nth 值创建数据框的迭代,然后删除第一个值并在行中添加下一个值

iteration that creates dataframe from 1:nth value, then deletes the first value and adds the next value in line

我有一个关于创建多个数据框的问题。下面是初始数据框。

framework <- structure(list(game_id = c(303731L, 303731L, 303731L, 303731L, 
303731L, 303731L, 303731L, 303731L, 303731L, 303731L, 303731L, 
303731L, 303731L, 303731L, 303731L, 303731L, 303731L, 303731L, 
303731L, 303731L), home_team = c(TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE), team_name = c("Barcelona", "Barcelona", 
"Barcelona", "Barcelona", "Barcelona", "Barcelona", "Barcelona", 
"Barcelona", "Barcelona", "Barcelona", "Barcelona", "Barcelona", 
"Barcelona", "Barcelona", "Barcelona", "Barcelona", "Barcelona", 
"Barcelona", "Barcelona", "Barcelona"), player_name = c("Antoine Griezmann", 
"Sergio Busquets i Burgos", "Sergio Busquets i Burgos", "Sergio Busquets i Burgos", 
"Arturo Erasmo Vidal Pardo", "Héctor Junior Firpo Adames", "Héctor Junior Firpo Adames", 
"Arturo Erasmo Vidal Pardo", "Arturo Erasmo Vidal Pardo", "Jordi Alba Ramos", 
"Arthur Henrique Ramos de Oliveira Melo", "Gerard Piqué Bernabéu", 
"Arthur Henrique Ramos de Oliveira Melo", "Samuel Yves Umtiti", 
"Clément Lenglet", "Clément Lenglet", "Gerard Piqué Bernabéu", 
"Gerard Piqué Bernabéu", "Clément Lenglet", "Clément Lenglet"
), type_name = c("pass", "dribble", "dribble", "pass", "dribble", 
"dribble", "pass", "dribble", "pass", "dribble", "dribble", "dribble", 
"pass", "dribble", "dribble", "pass", "dribble", "pass", "dribble", 
"pass"), result_name = c("success", "success", "success", "success", 
"success", "success", "success", "success", "success", "success", 
"success", "success", "success", "success", "success", "success", 
"success", "success", "success", "success"), no_passes = c(1, 
1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 8), subs = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = 77131:77150, class = "data.frame")

输出

      game_id home_team team_name                            player_name type_name result_name no_passes subs
77131  303731      TRUE Barcelona                      Antoine Griezmann      pass     success         1    0
77132  303731      TRUE Barcelona               Sergio Busquets i Burgos   dribble     success         1    0
77133  303731      TRUE Barcelona               Sergio Busquets i Burgos   dribble     success         1    0
77134  303731      TRUE Barcelona               Sergio Busquets i Burgos      pass     success         2    0
77135  303731      TRUE Barcelona              Arturo Erasmo Vidal Pardo   dribble     success         2    0
77136  303731      TRUE Barcelona             Héctor Junior Firpo Adames   dribble     success         2    0
77137  303731      TRUE Barcelona             Héctor Junior Firpo Adames      pass     success         3    0
77138  303731      TRUE Barcelona              Arturo Erasmo Vidal Pardo   dribble     success         3    0
77139  303731      TRUE Barcelona              Arturo Erasmo Vidal Pardo      pass     success         4    0
77140  303731      TRUE Barcelona                       Jordi Alba Ramos   dribble     success         4    0
77141  303731      TRUE Barcelona Arthur Henrique Ramos de Oliveira Melo   dribble     success         4    0
77142  303731      TRUE Barcelona                  Gerard Piqué Bernabéu   dribble     success         4    0
77143  303731      TRUE Barcelona Arthur Henrique Ramos de Oliveira Melo      SUB      success         5    1
77144  303731      TRUE Barcelona                     Samuel Yves Umtiti   dribble     success         5    0
77145  303731      TRUE Barcelona                        Clément Lenglet   dribble     success         5    0
77146  303731      TRUE Barcelona                        Clément Lenglet      pass     success         6    0
77147  303731      TRUE Barcelona                  Gerard Piqué Bernabéu   dribble     success         6    0
77148  303731      TRUE Barcelona                  Gerard Piqué Bernabéu      pass     success         7    0
77149  303731      TRUE Barcelona                        Clément Lenglet   dribble     success         7    0
77150  303731      TRUE Barcelona                        Clément Lenglet      pass     success         8    0 

我现在想做的是对 no_passes 列进行迭代,创建多个数据帧。这个想法是第一个数据帧由 no_passes == 1 组成,朝向 no_passes == 4.

每个新数据帧都应删除第一个 no_passes 并添加下一个 no_passes 行(因此在本例中为第 11 个)。

因此,第二个数据框应该包括 no_passes == 2no_passes == 5

之间的所有内容

这应该迭代到最后一个 no_passes,即 8。

下面是前两个数据帧的预期输出

输出 1

      game_id home_team team_name                            player_name type_name result_name no_passes subs
77131  303731      TRUE Barcelona                      Antoine Griezmann      pass     success         1    0  
77132  303731      TRUE Barcelona               Sergio Busquets i Burgos   dribble     success         1    0  
77133  303731      TRUE Barcelona               Sergio Busquets i Burgos   dribble     success         1    0  
77134  303731      TRUE Barcelona               Sergio Busquets i Burgos      pass     success         2    0  
77135  303731      TRUE Barcelona              Arturo Erasmo Vidal Pardo   dribble     success         2    0  
77136  303731      TRUE Barcelona             Héctor Junior Firpo Adames   dribble     success         2    0  
77137  303731      TRUE Barcelona             Héctor Junior Firpo Adames      pass     success         3    0  
77138  303731      TRUE Barcelona              Arturo Erasmo Vidal Pardo   dribble     success         3    0  
77139  303731      TRUE Barcelona              Arturo Erasmo Vidal Pardo      pass     success         4    0  
77140  303731      TRUE Barcelona                       Jordi Alba Ramos   dribble     success         4    0  
77141  303731      TRUE Barcelona Arthur Henrique Ramos de Oliveira Melo   dribble     success         4    0  
77142  303731      TRUE Barcelona                  Gerard Piqué Bernabéu   dribble     success         4    0  

输出 2

      game_id home_team team_name                            player_name type_name result_name no_passes subs  
77134  303731      TRUE Barcelona               Sergio Busquets i Burgos      pass     success         2    0  
77135  303731      TRUE Barcelona              Arturo Erasmo Vidal Pardo   dribble     success         2    0  
77136  303731      TRUE Barcelona             Héctor Junior Firpo Adames   dribble     success         2    0  
77137  303731      TRUE Barcelona             Héctor Junior Firpo Adames      pass     success         3    0  1
77138  303731      TRUE Barcelona              Arturo Erasmo Vidal Pardo   dribble     success         3    0  
77139  303731      TRUE Barcelona              Arturo Erasmo Vidal Pardo      pass     success         4    0  
77140  303731      TRUE Barcelona                       Jordi Alba Ramos   dribble     success         4    0  
77141  303731      TRUE Barcelona Arthur Henrique Ramos de Oliveira Melo   dribble     success         4    0  
77142  303731      TRUE Barcelona                  Gerard Piqué Bernabéu   dribble     success         4    0  
77143  303731      TRUE Barcelona Arthur Henrique Ramos de Oliveira Melo      SUB      success         5    1  1
77144  303731      TRUE Barcelona                     Samuel Yves Umtiti   dribble     success         5    0  
77145  303731      TRUE Barcelona                        Clément Lenglet   dribble     success         5    0  

我当前的代码如下所示并给出以下输出:


# First, create an empty datalist.
datalist_HT <- list()

# Create the 4-pass frame (with all actions between as well) for Home team
  for(j in seq_len(nrow(framework))-4){
    
    # 1: Grab first 4 no_passes starting from j
    passes_j <- framework[j:(j+4), ]
    
# now store each dataframe in a list
datalist_HT[[j]] <- passes_j

}

# Output
      game_id home_team team_name               player_name type_name result_name no_passes subs 
77131  303731      TRUE Barcelona         Antoine Griezmann      pass     success         1    0
77132  303731      TRUE Barcelona  Sergio Busquets i Burgos   dribble     success         1    0
77133  303731      TRUE Barcelona  Sergio Busquets i Burgos   dribble     success         1    0
77134  303731      TRUE Barcelona  Sergio Busquets i Burgos      pass     success         2    0
77135  303731      TRUE Barcelona Arturo Erasmo Vidal Pardo   dribble     success         2    0

但是,我倾向于只获取前 4 个条目,而不是前 4 个中的所有条目 no_passes

总结

我希望我说得足够清楚,否则我真的不知道该怎么做。

slider 在这些情况下很有用

library(tidyverse)
library(slider)

framework %>% group_by(no_passes) %>%
  nest() %>%
  slide(~.x %>% unnest(data), .after = 3, .complete = T) %>%
  subset(map(., length) > 0)

[[1]]
# A tibble: 12 x 8
# Groups:   no_passes [4]
   no_passes game_id home_team team_name player_name                            type_name result_name  subs
       <dbl>   <int> <lgl>     <chr>     <chr>                                  <chr>     <chr>       <dbl>
 1         1  303731 TRUE      Barcelona Antoine Griezmann                      pass      success         0
 2         1  303731 TRUE      Barcelona Sergio Busquets i Burgos               dribble   success         0
 3         1  303731 TRUE      Barcelona Sergio Busquets i Burgos               dribble   success         0
 4         2  303731 TRUE      Barcelona Sergio Busquets i Burgos               pass      success         0
 5         2  303731 TRUE      Barcelona Arturo Erasmo Vidal Pardo              dribble   success         0
 6         2  303731 TRUE      Barcelona Héctor Junior Firpo Adames             dribble   success         0
 7         3  303731 TRUE      Barcelona Héctor Junior Firpo Adames             pass      success         0
 8         3  303731 TRUE      Barcelona Arturo Erasmo Vidal Pardo              dribble   success         0
 9         4  303731 TRUE      Barcelona Arturo Erasmo Vidal Pardo              pass      success         0
10         4  303731 TRUE      Barcelona Jordi Alba Ramos                       dribble   success         0
11         4  303731 TRUE      Barcelona Arthur Henrique Ramos de Oliveira Melo dribble   success         0
12         4  303731 TRUE      Barcelona Gerard Piqué Bernabéu                  dribble   success         0

[[2]]
# A tibble: 12 x 8
# Groups:   no_passes [4]
   no_passes game_id home_team team_name player_name                            type_name result_name  subs
       <dbl>   <int> <lgl>     <chr>     <chr>                                  <chr>     <chr>       <dbl>
 1         2  303731 TRUE      Barcelona Sergio Busquets i Burgos               pass      success         0
 2         2  303731 TRUE      Barcelona Arturo Erasmo Vidal Pardo              dribble   success         0
 3         2  303731 TRUE      Barcelona Héctor Junior Firpo Adames             dribble   success         0
 4         3  303731 TRUE      Barcelona Héctor Junior Firpo Adames             pass      success         0
 5         3  303731 TRUE      Barcelona Arturo Erasmo Vidal Pardo              dribble   success         0
 6         4  303731 TRUE      Barcelona Arturo Erasmo Vidal Pardo              pass      success         0
 7         4  303731 TRUE      Barcelona Jordi Alba Ramos                       dribble   success         0
 8         4  303731 TRUE      Barcelona Arthur Henrique Ramos de Oliveira Melo dribble   success         0
 9         4  303731 TRUE      Barcelona Gerard Piqué Bernabéu                  dribble   success         0
10         5  303731 TRUE      Barcelona Arthur Henrique Ramos de Oliveira Melo pass      success         0
11         5  303731 TRUE      Barcelona Samuel Yves Umtiti                     dribble   success         0
12         5  303731 TRUE      Barcelona Clément Lenglet                        dribble   success         0

[[3]]
# A tibble: 11 x 8
# Groups:   no_passes [4]
   no_passes game_id home_team team_name player_name                            type_name result_name  subs
       <dbl>   <int> <lgl>     <chr>     <chr>                                  <chr>     <chr>       <dbl>
 1         3  303731 TRUE      Barcelona Héctor Junior Firpo Adames             pass      success         0
 2         3  303731 TRUE      Barcelona Arturo Erasmo Vidal Pardo              dribble   success         0
 3         4  303731 TRUE      Barcelona Arturo Erasmo Vidal Pardo              pass      success         0
 4         4  303731 TRUE      Barcelona Jordi Alba Ramos                       dribble   success         0
 5         4  303731 TRUE      Barcelona Arthur Henrique Ramos de Oliveira Melo dribble   success         0
 6         4  303731 TRUE      Barcelona Gerard Piqué Bernabéu                  dribble   success         0
 7         5  303731 TRUE      Barcelona Arthur Henrique Ramos de Oliveira Melo pass      success         0
 8         5  303731 TRUE      Barcelona Samuel Yves Umtiti                     dribble   success         0
 9         5  303731 TRUE      Barcelona Clément Lenglet                        dribble   success         0
10         6  303731 TRUE      Barcelona Clément Lenglet                        pass      success         0
11         6  303731 TRUE      Barcelona Gerard Piqué Bernabéu                  dribble   success         0

[[4]]
# A tibble: 11 x 8
# Groups:   no_passes [4]
   no_passes game_id home_team team_name player_name                            type_name result_name  subs
       <dbl>   <int> <lgl>     <chr>     <chr>                                  <chr>     <chr>       <dbl>
 1         4  303731 TRUE      Barcelona Arturo Erasmo Vidal Pardo              pass      success         0
 2         4  303731 TRUE      Barcelona Jordi Alba Ramos                       dribble   success         0
 3         4  303731 TRUE      Barcelona Arthur Henrique Ramos de Oliveira Melo dribble   success         0
 4         4  303731 TRUE      Barcelona Gerard Piqué Bernabéu                  dribble   success         0
 5         5  303731 TRUE      Barcelona Arthur Henrique Ramos de Oliveira Melo pass      success         0
 6         5  303731 TRUE      Barcelona Samuel Yves Umtiti                     dribble   success         0
 7         5  303731 TRUE      Barcelona Clément Lenglet                        dribble   success         0
 8         6  303731 TRUE      Barcelona Clément Lenglet                        pass      success         0
 9         6  303731 TRUE      Barcelona Gerard Piqué Bernabéu                  dribble   success         0
10         7  303731 TRUE      Barcelona Gerard Piqué Bernabéu                  pass      success         0
11         7  303731 TRUE      Barcelona Clément Lenglet                        dribble   success         0

[[5]]
# A tibble: 8 x 8
# Groups:   no_passes [4]
  no_passes game_id home_team team_name player_name                            type_name result_name  subs
      <dbl>   <int> <lgl>     <chr>     <chr>                                  <chr>     <chr>       <dbl>
1         5  303731 TRUE      Barcelona Arthur Henrique Ramos de Oliveira Melo pass      success         0
2         5  303731 TRUE      Barcelona Samuel Yves Umtiti                     dribble   success         0
3         5  303731 TRUE      Barcelona Clément Lenglet                        dribble   success         0
4         6  303731 TRUE      Barcelona Clément Lenglet                        pass      success         0
5         6  303731 TRUE      Barcelona Gerard Piqué Bernabéu                  dribble   success         0
6         7  303731 TRUE      Barcelona Gerard Piqué Bernabéu                  pass      success         0
7         7  303731 TRUE      Barcelona Clément Lenglet                        dribble   success         0
8         8  303731 TRUE      Barcelona Clément Lenglet                        pass      success         0

如果要将列表的每个项目保存为单独的输出,请先使用 setNames 然后使用 list2env 将每个项目保存为单独的 df in envir = .GlobalEnv