0

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?

user3701522
  • 307
  • 3
  • 12

1 Answers1

1

Using data.table approach:

require(data.table)
x <- c("A", "B", "A", "C", "B", "A", "D")
dt <- data.table(x)
dt[,RANK := seq_len(.N), by = x]
Tobias Dekker
  • 980
  • 8
  • 19
  • This works. However, I am new to R and have not come across seq_len(.N). Can you please point me to the relevant documentation on this? – user3701522 Apr 25 '17 at 12:59
  • It is very simple seq_len(x) gives you a sequence from 1 to x, the .N is data table syntax and counts the number of rows – Tobias Dekker Apr 25 '17 at 13:11