I have a data frame in which I want to add an index e.g. 1...n for each factor in my data frame. Here is an example with some dummy data.
factor
a
a
One way is:
unlist(lapply(split(x, x), seq_along))
where x is your factor as a vector.
R> x <- factor(rep(letters[1:3], times = c(5,5,4))) ## your data
R> data.frame(factor = x, index = unlist(lapply(split(x, x), seq_along),
+ use.names = FALSE))
factor index
1 a 1
2 a 2
3 a 3
4 a 4
5 a 5
6 b 1
7 b 2
8 b 3
9 b 4
10 b 5
11 c 1
12 c 2
13 c 3
14 c 4
Another way, on a similar theme is to use table() and seq_len():
unlist(sapply(table(x), seq_len), use.names = FALSE)
And another way is to use the run-length encoding via rle():
R> rle(as.character(x))$lengths
[1] 5 5 4
which we can plug into the sapply() code instead of the table() call:
R> unlist(sapply(rle(as.character(x))$lengths, seq_len), use.names = FALSE)
[1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4