Christian
Christian

Reputation: 149

Create a Column that Counts (one at a time) Repeated Occurrences Within Groups? (in R)

mydata <- read.table(header=TRUE, text="
  Away    Home   Game.ID   Points.A   Points.H   Series.ID   Series.Wins   
  Denver  Utah   aaa123      121        123       aaabbb          Utah
  Denver  Utah   aaa124      132        116       aaabbb          Denver
  Utah    Denver aaa125      117        121       aaabbb          Denver
  Utah    Denver aaa126      112        120       aaabbb          Denver
  Denver  Utah   aaa127      115        122       aaabbb          Utah
  Atlanta Boston aab123      112        114       aaaccc          Boston
")

I am trying to create an additional column that counts, one at a time, the Series.Wins within each Series.ID group. So, from the data above, that column column would look like:

new.column <- c(1, 1, 2, 3, 2, 1)

The ultimate goal is to come up with a series record column with "home wins - away wins":

Record <- c(1-0, 1-1, 2-1, 3-1, 2-3, 1-0)

Upvotes: 1

Views: 23

Answers (1)

Christian
Christian

Reputation: 149

This seemed to work:

mydata <- mydata %>% group_by(Series.Wins) %>% group_by(Series.ID, add = TRUE) %>% mutate(id = seq_len(n()))

Upvotes: 1

Related Questions