简体   繁体   中英

Creating unique count variable for each row based on group_by variable

I have the below data frame:

help <- data.frame(id = c(5,5,5,5,5,12,12,12,17,17,20,20,20,20,20,20),
                   number = c(1,2,3,4,5,1,2,3,1,2,1,2,3,4,5,6),
                   episode = c(1,1,1,2,2,1,1,1,1,1,1,1,1,2,2,3))


   id number episode
1   5      1       1
2   5      2       1
3   5      3       1
4   5      4       2
5   5      5       2
6  12      1       1
7  12      2       1
8  12      3       1
9  17      1       1
10 17      2       1
11 20      1       1
12 20      2       1
13 20      3       1
14 20      4       2
15 20      5       2
16 20      6       3

I have a number of observations variable number for each id, but also want a unique count within each episode.

My hope is for the df to look like

   id number episode number.ep
1   5      1       1         1
2   5      2       1         2
3   5      3       1         3
4   5      4       2         1
5   5      5       2         2
6  12      1       1         1
7  12      2       1         2
8  12      3       1         3
9  17      1       1         1
10 17      2       1         2
11 20      1       1         1
12 20      2       1         2
13 20      3       1         3
14 20      4       2         1
15 20      5       2         2
16 20      6       3         1

I'm hitting errors within my mutate command after using group_by(id). Any suggestions?

using dplyr:

library(dplyr)

help %>% group_by(id, episode) %>%
         mutate(number.ep = row_number( ))

This could be an option

library(dplyr)
help %>% group_by(id, episode) %>% 
         mutate(number.ep = seq(1, length(episode), by = 1))

#   id number episode number.ep
#1   5      1       1         1
#2   5      2       1         2
#3   5      3       1         3
#4   5      4       2         1
#5   5      5       2         2
#6  12      1       1         1
#7  12      2       1         2
#8  12      3       1         3
#9  17      1       1         1
#10 17      2       1         2
#11 20      1       1         1
#12 20      2       1         2
#13 20      3       1         3
#14 20      4       2         1
#15 20      5       2         2
#16 20      6       3         1

equivalent of this using data.table would be

library(data.table)
setDT(help)[, number.ep := seq(.N), by = .(id, episode)]

#> help
#    id number episode number.ep
# 1:  5      1       1         1
# 2:  5      2       1         2
# 3:  5      3       1         3
# 4:  5      4       2         1
# 5:  5      5       2         2
# 6: 12      1       1         1
# 7: 12      2       1         2
# 8: 12      3       1         3
# 9: 17      1       1         1
#10: 17      2       1         2
#11: 20      1       1         1
#12: 20      2       1         2
#13: 20      3       1         3
#14: 20      4       2         1
#15: 20      5       2         2
#16: 20      6       3         1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM