-5

I have a question about numbering rows by group AND by one further condition. I know how to do this by group but not by adding one further condition.

Suppose I have the ID and the DATE and want to create NUM as shown in the table:

ID ...... DATE...... NUM

1 20160103 ...... 1
1 20160104...... 1
1 20160104...... 2
1 20160105...... 1
1 20160105...... 2
1 20160105...... 3
1 20160106...... 1
2 20160103...... 1
2 20160103...... 2
2 20160105...... 1

Any one knows How to do this?

1 Answers1

0

We can use ave from base R

 df$NUM <- with(df, ave(ID, ID, DATE, FUN =seq_along))
akrun
  • 874,273
  • 37
  • 540
  • 662