Creating a column to list multiple events

I have data on a multitude of events that occur in some order. I need a column that lists this set of events: this row is the first, this row is the second time, etc. The data will look like this:

S Time A 3 A 4 A 5 A 10 B 4 B 9 B 1 B 37 

Where S is some kind of session identifier, and time is time. I would like to add the following column of results:

 S Time Order A 3 1 A 5 3 A 4 2 A 10 4 B 4 2 B 9 3 B 1 1 B 37 4 

For each of the session identifiers, I want to have a column that arranges rows by time. I am using dplyr and I hope for a short way to do this.

+5
source share
2 answers

Try

  library(dplyr) df1 %>% group_by(S) %>% mutate(Order=rank(Time)) 
+7
source

You can also do:

 df %>% group_by(S) %>% mutate(Order = row_number(Time)) 

In the dplyr package dplyr row_number() equivalent to rank(ties.method = "first")

+5
source

All Articles