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

C.F.

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.

Shree

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Assign unique ID to distinct values within Group with dplyr

分類Dev

Select groups with more than one distinct value per group

分類Dev

How to use lookahead and lookbehind in more than one capturing group

分類Dev

Creating more than one class

分類Dev

Creating more than one initial route

分類Dev

Creating more than one array for dataset

分類Dev

creating indexer for a class with more than one array?

分類Dev

Creating a new column and assigning values if any one of the row within a group contains a certain value

分類Dev

Select only the least unique value of 2 consecutive rows within a group

分類Dev

DataFrame: Group by one column and average other columns

分類Dev

How to group by ID and expand dataframe by another column

分類Dev

Group all the similar id as one in jquery

分類Dev

R:Plotly - Creating Multiple boxplots in one graph as a group

分類Dev

Creating a new group

分類Dev

Rewrite automatically removes backslash if there's more than one?

分類Dev

Grouping DataFrame, filtering by group size and by value of column in one line?

分類Dev

dataframe columns as key and column data as value group by id in spark scala

分類Dev

Inject or interpolate more than one ENV variable in go build command within dockerfile

分類Dev

What is the benefit to having more than one column in a non-unique index?

分類Dev

How to find minimum within group value greater than certain value in r

分類Dev

Group by / Aggregate Pandas Dataframe

分類Dev

Filter group by result in DataFrame

分類Dev

Pandas: Groupby and cut within a group

分類Dev

lm() within mutate() in group_by()

分類Dev

Using EXISTS within a GROUP BY clause

分類Dev

Creating an ALB Target Group in CloudFormation

分類Dev

More than one Intent ExtraText?

分類Dev

More than one Navigation controller

分類Dev

Listen to more than one KeyphraseSearch

Related 関連記事

  1. 1

    Assign unique ID to distinct values within Group with dplyr

  2. 2

    Select groups with more than one distinct value per group

  3. 3

    How to use lookahead and lookbehind in more than one capturing group

  4. 4

    Creating more than one class

  5. 5

    Creating more than one initial route

  6. 6

    Creating more than one array for dataset

  7. 7

    creating indexer for a class with more than one array?

  8. 8

    Creating a new column and assigning values if any one of the row within a group contains a certain value

  9. 9

    Select only the least unique value of 2 consecutive rows within a group

  10. 10

    DataFrame: Group by one column and average other columns

  11. 11

    How to group by ID and expand dataframe by another column

  12. 12

    Group all the similar id as one in jquery

  13. 13

    R:Plotly - Creating Multiple boxplots in one graph as a group

  14. 14

    Creating a new group

  15. 15

    Rewrite automatically removes backslash if there's more than one?

  16. 16

    Grouping DataFrame, filtering by group size and by value of column in one line?

  17. 17

    dataframe columns as key and column data as value group by id in spark scala

  18. 18

    Inject or interpolate more than one ENV variable in go build command within dockerfile

  19. 19

    What is the benefit to having more than one column in a non-unique index?

  20. 20

    How to find minimum within group value greater than certain value in r

  21. 21

    Group by / Aggregate Pandas Dataframe

  22. 22

    Filter group by result in DataFrame

  23. 23

    Pandas: Groupby and cut within a group

  24. 24

    lm() within mutate() in group_by()

  25. 25

    Using EXISTS within a GROUP BY clause

  26. 26

    Creating an ALB Target Group in CloudFormation

  27. 27

    More than one Intent ExtraText?

  28. 28

    More than one Navigation controller

  29. 29

    Listen to more than one KeyphraseSearch

ホットタグ

アーカイブ