I have a data frame with two columns: the first column contains the group to which each person belongs, and the second contains an individual identifier. See below:
df <- data.frame( group=c('G1','G1','G1','G1','G2','G2','G2','G2'),
indiv=c('indiv1','indiv1','indiv2','indiv2','indiv3',
'indiv3','indiv4','indiv4'))
group indiv
1 G1 indiv1
2 G1 indiv1
3 G1 indiv2
4 G1 indiv2
5 G2 indiv3
6 G2 indiv3
7 G2 indiv4
8 G2 indiv4
I would like to create a new column in my data frame (keeping a long format) with the index of each person in the group, that is:
group indiv Ineed
1 G1 indiv1 1
2 G1 indiv1 1
3 G1 indiv2 2
4 G1 indiv2 2
5 G2 indiv3 1
6 G2 indiv3 1
7 G2 indiv4 2
8 G2 indiv4 2
I tried with the data.table.N or .GRP methods, without success (good work on data.table, by the way!).
Any help is much appreciated!
source
share