Consider the following dataset:
df = data.frame(id = c(1,1,1,2,2,2,3,3,3),
time = c(1,2,3,1,2,3,1,2,3),
x = c(8,8,9,7,7,7,7,7,8),
id_x = c(1,1,2,3,3,3,4,4,5))
I want to compute id_x
which identifies each unique combination of variables id
and x
(preferably using dplyr
).
In Stata, I can do the following:
Stata
clear
input id time x
1 1 8
1 2 8
1 3 9
2 1 7
2 2 7
2 3 7
3 1 7
3 2 7
3 3 8
end
egen id_x = group(id, x)
list, separator(0)
+----------------------+
| id time x id_x |
|----------------------|
1. | 1 1 8 1 |
2. | 1 2 8 1 |
3. | 1 3 9 2 |
4. | 2 1 7 3 |
5. | 2 2 7 3 |
6. | 2 3 7 3 |
7. | 3 1 7 4 |
8. | 3 2 7 4 |
9. | 3 3 8 5 |
+----------------------+
group_indices(id, x)
... still in theStata
mindset. Thanks! – Correction