I have a longitudinal data in a long format. I want to create an ID variable based on the variable-column that identifies each observation of my data. How do I do that in R?
Example: I have this data
name year var1 var2
A 1 4 3
A 2 5 1
A 3 4 2
B 1 . .
B 2 4 3
B 3 5 1
I want to produce a new column called 'id' with a unique number for every name, such as:
name id year var1 var2
A 1 1 4 3
A 1 2 5 1
A 1 3 4 2
B 2 1 . .
B 2 2 4 3
B 2 3 5 1
Any help?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…