0
df <- data.frame(Semester = c(rep(1, each=8), rep(2, each=6), rep(3, each=5)))

I want to make a second column, Student, that for every semester, goes 1, 2, 3,... and then resets for the next semester. That is, it creates the following:

df <- data.frame(Semester = c(rep(1, each=8), rep(2, each=6), rep(3, each=5)),
                 Student = c(1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5))

just from the Semester column. How do I do this without a for loop?

Forklift17
  • 2,245
  • 3
  • 20
  • 32
  • `dplyr::mutate(df, Student = dplyr::row_number(), .by = Semester)`. Requires dplyr >= v1.1.0; otherwise use `group_by()` instead of `.by`. – zephryl Apr 12 '23 at 21:25

0 Answers0