Search code examples
rdata-cleaningdata-wrangling

Creating a variable that appends together strings


I have a data frame like so:

moves game_id
e2e4 1
cfc8 1
g6e4 1
f6g8 2
f8g5 2
e2e4 3
d8b6 3
h7a3 3

I want to create a column which appends each previous string in 'moves' to a list that grows, but restarts for the next 'game_id'.

So that I create a table like so:

moves game_id move_list
e2e4 1 e2e4
cfc8 1 e2e4 cfc8
g6e4 1 e2e4 cfc8 g6e4
f6g8 2 f6g8
f8g5 2 f6g8 f8g5
e2e4 3 e2e4
d8b6 3 e2e4 d8b6
h7a3 3 e2e4 d8b6 h7a3

Thanks in advance for the help!


Solution

  • We may use accumulate after grouping

    library(dplyr)
    library(purrr)
    library(stringr)
    df1 %>% 
     group_by(game_id) %>%
      mutate(move_list  = accumulate(moves, str_c, sep = " ")) %>% 
      ungroup
    

    -output

    # A tibble: 8 × 3
      moves game_id move_list     
      <chr>   <int> <chr>         
    1 e2e4        1 e2e4          
    2 cfc8        1 e2e4 cfc8     
    3 g6e4        1 e2e4 cfc8 g6e4
    4 f6g8        2 f6g8          
    5 f8g5        2 f6g8 f8g5     
    6 e2e4        3 e2e4          
    7 d8b6        3 e2e4 d8b6     
    8 h7a3        3 e2e4 d8b6 h7a3
    

    If the output should be a list column, use c/append instead of pasteing

    df1 %>% 
      group_by(game_id) %>% 
      mutate(move_list  = accumulate(moves,  c)) %>% 
      ungroup
    # A tibble: 8 × 3
      moves game_id move_list
      <chr>   <int> <list>   
    1 e2e4        1 <chr [1]>
    2 cfc8        1 <chr [2]>
    3 g6e4        1 <chr [3]>
    4 f6g8        2 <chr [1]>
    5 f8g5        2 <chr [2]>
    6 e2e4        3 <chr [1]>
    7 d8b6        3 <chr [2]>
    8 h7a3        3 <chr [3]>
    

    data

    df1 <- structure(list(moves = c("e2e4", "cfc8", "g6e4", "f6g8", "f8g5", 
    "e2e4", "d8b6", "h7a3"), game_id = c(1L, 1L, 1L, 2L, 2L, 3L, 
    3L, 3L)), class = "data.frame", row.names = c(NA, -8L))