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
  • 23,996
  • 4
  • 21
  • 40
kamran khan
  • 311
  • 1
  • 7

2 Answers2

1

We can use rowid

library(data.table)
df1$Number <- rowid(df1$PatID)
akrun
  • 789,025
  • 32
  • 460
  • 575
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
  • 23,996
  • 4
  • 21
  • 40