C.F.
C.F.

Reputation: 294

Creating unique group id's within a dataframe with more than one group

I have a dataframe:

y <- c(3, 3, 3, 2, 2, 2, 2, 1, 1, 2)
z <- c(1, 1, 1, 2, 2, 3, 3, 3, 4, 4)

df <- data.frame(y, z)

> df
   y z
1  3 1
2  3 1
3  3 1
4  2 2
5  2 2
6  2 3
7  2 3
8  1 3
9  1 4
10 2 4

Now i want to create a group-id. The groups are based on y and should be numbered from 1 to n. Repetitive numbers for y refer to one group. In addition, the groups are nested in other groups based on z, so equal numbers for y represent different groups if they are in different groups at z. That means: for y there are 6 groups, for z 4 groups. Result should be:

> df
   y z group_id
1  3 1        1
2  3 1        1
3  3 1        1
4  2 2        2
5  2 2        2
6  2 3        3
7  2 3        3
8  1 3        4
9  1 4        5
10 2 4        6

I am happy about any help.

Upvotes: 1

Views: 38

Answers (2)

akrun
akrun

Reputation: 887118

We can use rleid from data.table

library(data.table)
setDT(df)[, group_id := rleid(y, z)]

Upvotes: 1

Shree
Shree

Reputation: 11140

You can use rleid from data.table package -

df$group_id <- data.table::rleid(paste(df$y, df$z))
df
   y z group_id
1  3 1        1
2  3 1        1
3  3 1        1
4  2 2        2
5  2 2        2
6  2 3        3
7  2 3        3
8  1 3        4
9  1 4        5
10 2 4        6

Upvotes: 1

Related Questions