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
  • 127
  • 1
  • 7
  • 3
    `df$id <- with(df, as.numeric(factor(subject, levels = unique(subject))))` – rawr Aug 05 '14 at 23:50
  • 1
    I always find `rle` useful for this kind of thing.... `r <- rle(df$subject); rep(seq_along(r$values),r$lengths)` – Simon O'Hanlon Aug 06 '14 at 00:22
  • 4
    @rawr, why do you so often answer questions as a comment and not write a full answer? I see you do this a lot (700+ comments for only 75 answers) and I find it a bit counter productive for everyone. – flodel Aug 06 '14 at 00:42

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
  • 8,191
  • 8
  • 72
  • 128