0

I have an existing data.table that I am wanting to add an 'identity' column to.

I know how to add a column e.g:

DT$newCol <- x

However I am struggling to make the new value an auto-incrementing counter from 1 to n.

I would have asked as a comment to this question (data.table "key indices" or "group counter") as it is related, however I don't have enough reputation to comment.

Thanks in advance.

Community
  • 1
  • 1
Dan
  • 2,625
  • 5
  • 27
  • 42

1 Answers1

1

In the spirit of completeness in case someone stumbles across this.

DT[, newcol := 1:.N] 

is how I solved the problem. Thanks to go @thelatemail and @Simon

Dan
  • 2,625
  • 5
  • 27
  • 42