Reputation: 683
I have only used the rle function to run queries that give me the n() of a condition but now I need to find consecutive streaks of one variable and group each streak together so I can sum the total of another column. dput example given to show what I mean below.
What I want to find is the amount of consecutive points each player scores just for his team. The data below is a chronological order of plays and "possession" indicates which of the two teams scored the shot. "player_fg" shows which player that was. "pts" is the column I want to add for each player. For Example, Scottie Pippen scored the final four shots for the Chicago Bulls and thus the answer should be 8 total points for him.
I can write a rle code that adds one for each consecutive player on the same team that makes a shot but it doesn't add all of those made shots together.
df %>%
group_by(gameid, possession) %>%
mutate(marker = ifelse(player_fg != lag(player_fg), 1,
sequence(rle(player_fg)$lengths))) %>%
view
That's where I get stuck.
dput for reproducibility:
structure(list(season = c("96/97", "96/97", "96/97", "96/97",
"96/97", "96/97", "96/97", "96/97", "96/97", "96/97"), gameid =
c("ChicagoBoston19961101",
"ChicagoBoston19961101", "ChicagoBoston19961101", "ChicagoBoston19961101",
"ChicagoBoston19961101", "ChicagoBoston19961101", "ChicagoBoston19961101",
"ChicagoBoston19961101", "ChicagoBoston19961101", "ChicagoBoston19961101"
), possession = c("Chicago", "Chicago", "Boston", "Boston", "Chicago",
"Chicago", "Chicago", "Boston", "Boston", "Chicago"), pts = c(2,
1, 2, 2, 2, 2, 2, 2, 1, 2), player_fg = c("D. Rodman", "D. Rodman",
"D. Wesley", "E. Williams", "S. Pippen", "S. Pippen", "S. Pippen",
"P. Ellison", "P. Ellison", "S. Pippen")), row.names = c(NA,
-10L), class = "data.frame")
Upvotes: 0
Views: 28