Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grouped Sequences in dplyr | Alan Yeung #5

Open
utterances-bot opened this issue Nov 8, 2023 · 2 comments
Open

Grouped Sequences in dplyr | Alan Yeung #5

utterances-bot opened this issue Nov 8, 2023 · 2 comments
Labels
comment blog post comments

Comments

@utterances-bot
Copy link

Grouped Sequences in dplyr | Alan Yeung

For a piece of work I had to calculate the number of matches that a team plays away from home in a row, which we will call days_on_the_road. I was not sure how to do this with dplyr but it’s basically a ‘grouped sequence’. For this post, I’ve created some dummy data to illustrate this idea. The num_matches_away variable is what we want to mimic using some data manipulation.

https://alan-y.netlify.com/post/2023-11-05-grouped-sequences-in-dplyr/

Copy link

Hi Alan
I got this short solution:
sports_df2 %>%
group_by(team) %>%

cumulated counts of runs of A and H

mutate(RLE = sequence( rle(home_or_away)$lengths) ) %>%

make H numbers zero

mutate(RLE = ifelse(home_or_away == "A", RLE, 0) ) %>%
ungroup()

Hope this helps
Ilkka Havukkala

Copy link
Owner

alan-y commented Nov 23, 2023

Thanks @havukkala, that's a really neat solution!

@alan-y alan-y added the comment blog post comments label Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comment blog post comments
Projects
None yet
Development

No branches or pull requests

3 participants