rdataframedplyr

Adding a row at the end of a group in R


Suppose I have this dataframe, df:

 UserID <- c(1, 1, 1, 5, 5)
 PathID <- c(1,2,3,1,2)
 Page <- c("home", "about", "services", "home", "pricing")
 df <- data.frame(UserID, PathID, Page)

       UserID PathID  Page
    1  1      1       home
    2  1      2       about
    3  1      3       services
    4  5      1       home
    5  5      2       pricing

I am trying to add a new row at the end of each PathID. I am using dplyr and part of my code is below:

group_by(UserID) %>%
summarise(Page,
          PathID = row_number())

I would like my dataframe output to look like this:

   UserID PathID  Page
1  1      1       home
2  1      2       about
3  1      3       services
4  1      4       end
4  5      1       home
5  5      2       pricing
6  5      3       end

Any help is much appreciated. Thank you.


Solution

  • With dplyr, you could use group_modify + add_row:

    library(dplyr)
    
    df %>%
      group_by(UserID) %>%
      group_modify(~ .x %>%
        summarise(PathID = max(PathID) + 1, Page = "end") %>%
        add_row(.x, .)
      ) %>%
      ungroup()
    
    # # A tibble: 7 × 3
    #   UserID PathID Page
    #    <dbl>  <dbl> <chr>
    # 1      1      1 home
    # 2      1      2 about
    # 3      1      3 services
    # 4      1      4 end
    # 5      5      1 home
    # 6      5      2 pricing
    # 7      5      3 end