I have the following code
> x <- c("A", "B", "A", "C", "B", "A", "D")
> df <- data.frame(x)
> df
x
1 A
2 B
3 A
4 C
5 B
6 A
7 D
>
I eventually want to mark the data frame rows with another column so that each instance of x is tagged with it's rank like so
1 A 1
2 B 1
3 A 2
4 C 1
5 B 2
6 A 3
7 D 1
Here's how I have been thinking, but I am not sure if this is the best way
> df$y <- sapply(df$x, function(x){order(which(df$x==x))});
> df
x y
1 A 1, 2, 3
2 B 1, 2
3 A 1, 2, 3
4 C 1
5 B 1, 2
6 A 1, 2, 3
7 D
So, essentially, I now have a data frame with each of the 'x's along with the indexes in sequential manner.
How do I proceed from here without writing an explicit loop? Is there a better approach?