Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a row at the end of a group in R

Tags:

dataframe

r

dplyr

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.

like image 510
user2845095 Avatar asked Oct 25 '25 19:10

user2845095


2 Answers

You can use pick() within reframe() to operate on the group data:

library(tidyverse)

df %>%
  group_by(UserID) %>%
  reframe(
    pick(everything()) %>% 
      add_row(PathID = max(PathID) + 1, Page = "end")
  )
#> # 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

The method below was deprecated in dplyr 1.1.0 (2023-03-22).

You can also summarise() directly using cur_data():

library(tidyverse)

df %>%
  group_by(UserID) %>%
  summarise(
    cur_data() %>% 
      add_row(PathID = max(PathID) + 1, Page = "end")
  )
#> # A tibble: 7 × 3
#> # Groups:   UserID [2]
#>   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
like image 94
Mikko Marttila Avatar answered Oct 27 '25 11:10

Mikko Marttila


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
like image 35
Darren Tsai Avatar answered Oct 27 '25 10:10

Darren Tsai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!