Adding a counter column to a set of similar rows in R

I have a data frame in R with two columns. The first column contains the identifier of the subject, and the second column contains the trial identifier that was made by the subject.

A particular subject ID could conduct a study more than once. I want to add a column with a counter that will start counting each unique value for each object and increment by 1 until it reaches the last row with this event.

More precisely, I have this table:

ID T A 1 A 1 A 2 A 2 B 1 B 1 B 1 B 1 

and I need the following output

 ID T Index A 1 1 A 1 2 A 2 1 A 2 2 B 1 1 B 1 2 B 1 3 B 1 4 
+6
source share
1 answer

I really like the simple data.table syntax for this (not to mention speed) ...

 # Load package require( data.table ) # Turn data.frame into a data.table dt <- data.table( df ) # Get running count by ID and T dt[ , Index := 1:.N , by = c("ID" , "T") ] # ID T Index #1: A 1 1 #2: A 1 2 #3: A 2 1 #4: A 2 2 #5: B 1 1 #6: B 1 2 #7: B 1 3 #8: B 1 4 

.N is an integer equal to the number of lines in each group. Groups are defined by column names in the by argument, so 1:.N gives the vector as long as the group.

As data.table inherits from data.frame any function that accepts the input data.frame , also accepts the input data.table as an input, and you can easily convert back if you want ( df <- data.frame( dt ) )

+8
source

Source: https://habr.com/ru/post/957634/


All Articles