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.