0

I have a dataset like this

df=data.frame(subject= c(rep(1001, 3), rep(732, 2),rep(966,4)))

I wish to create an ID column so that the output could look like this

  subject id
1    1001  1
2    1001  1
3    1001  1
4     732  2
5     732  2
6     966  3
7     966  3
8     966  3
9     966  3

I used the code df$id <- as.numeric(as.factor(df$subject)) , but it gave me an id column ordered by the subject number like this

  subject id
1    1001  3
2    1001  3
3    1001  3
4     732  1
5     732  1
6     966  2
7     966  2
8     966  2
9     966  2

does anyone know how to make the id column with its natural order?

dzadi
  • 107
  • 1
  • 7

1 Answers1

2

Simply coping rawr's comment to get the question closed (I'm happy to delete this answer if rawr post an answer)

df=data.frame(subject= c(rep(1001, 3), rep(732, 2),rep(966,4)))
df
  subject id
1    1001  1
2    1001  1
3    1001  1
4     732  2
5     732  2
6     966  3
7     966  3
8     966  3
9     966  3
Eric Fail
  • 7,757
  • 6
  • 65
  • 120