2021年5月1日星期六

How to Treat A grouping Variable ID as multiple groups if observations are non-consecutive?

I am working with the following data frame: enter image description here I am wondering how I can treat an ID as multiple different grouping variables if the observations are non-consecutive. So with the above data frame, how can I create the following result? Note how "IndividualID" is treated as different groups if the results are non-consecutive. enter image description here

Right now I am using this code:

data <- data %>%    arrange(Team, Game, Minute, IndividualID) %>%    group_(Team, Game, IndividualID) %>%    mutate(Count = row_number())  

But of course this does not produce the intended result.

structure(list(Team = c("A", "A", "A", "A", "B", "B", "B", "B",   "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"), Game = c(1L,   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,   2L), Minute = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L,   2L, 3L, 4L, 5L, 6L, 7L), IndividualID = c(456L, 456L, 322L, 456L,   456L, 456L, 322L, 322L, 456L, 456L, 456L, 222L, 222L, 115L, 115L,   222L, 222L, 222L), Count = c(1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L,   1L, 2L, 3L, 1L, 2L, 1L, 2L, 1L, 2L, 3L)), class = "data.frame", row.names = c(NA,   -18L))  
https://stackoverflow.com/questions/67352408/how-to-treat-a-grouping-variable-id-as-multiple-groups-if-observations-are-non-c May 02, 2021 at 10:09AM

没有评论:

发表评论