0

Table

How to I create the number column as in the table attached, which look into the Admi_num column and check if its the first admission and then put a 1 in the number column and similarly put a 2 ,3 and so on values based on the administration_num column. Thank you

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
kam
  • 345
  • 1
  • 8

2 Answers2

1

We can use rowid

library(data.table)
df1$Number <- rowid(df1$PatID)
akrun
  • 874,273
  • 37
  • 540
  • 662
1

If say your data name is df, this base R way should yield similar results

ave(df$PatID, df$PatID, FUN = function(x) seq_len(length(x)))
AnilGoyal
  • 25,297
  • 4
  • 27
  • 45