0

I have a dataframe and I need to give each group an ID. A group is defined as long as the value is the same.

here is the dataframe

structure(list(groups = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 4L, 4L, 4L, 4L, 1L, 1L, 3L, 3L, 3L, 3L, 
2L, 2L, 2L, 2L, 2L), .Label = c("A", "B", "C", "F"), class = "factor"), 
    type = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
    2L, 1L, 1L, 4L, 4L, 4L, 4L, 1L, 1L, 3L, 3L, 3L, 3L, 2L, 2L, 
    2L, 2L, 2L), .Label = c("Apple", "Bread", "curry", "Fish"
    ), class = "factor")), class = "data.frame", row.names = c(NA, 
-28L))

And the result should look like this:

group ID
A 1
A 1
A 1
A 1
A 1
B 2
B 2
B 2
B 2
B 2
B 2
A 3
A 3
F 4
F 4
F 4
F 4
A 4
A 4
C 5
C 5
C 5
C 5
B 6
B 6
B 6
B 6
B 6

thx for your help!

pampi
  • 37
  • 6

2 Answers2

1

Use rleid in the data.table package:

library(data.table)
transform(dat, ID = rleid(groups), type = NULL)

giving:

   groups ID
1       A  1
2       A  1
3       A  1
4       A  1
5       A  1
6       B  2
7       B  2
8       B  2
9       B  2
10      B  2
11      B  2
12      A  3
13      A  3
14      F  4
15      F  4
16      F  4
17      F  4
18      A  5
19      A  5
20      C  6
21      C  6
22      C  6
23      C  6
24      B  7
25      B  7
26      B  7
27      B  7
28      B  7
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
0

A tidyverse option.

library(dplyr)
library(tidyr)

mutate(df, groupID = cumsum(replace_na(lag(groups) != groups, FALSE)) + 1)

#    groups  type groupID
# 1       A Apple       1
# 2       A Apple       1
# 3       A Apple       1
# 4       A Apple       1
# 5       A Apple       1
# 6       B Bread       2
# 7       B Bread       2
# 8       B Bread       2
# 9       B Bread       2
# 10      B Bread       2
# 11      B Bread       2
# 12      A Apple       3
# 13      A Apple       3
# 14      F  Fish       4
# 15      F  Fish       4
# 16      F  Fish       4
# 17      F  Fish       4
# 18      A Apple       5
# 19      A Apple       5
# 20      C curry       6
# 21      C curry       6
# 22      C curry       6
# 23      C curry       6
# 24      B Bread       7
# 25      B Bread       7
# 26      B Bread       7
# 27      B Bread       7
# 28      B Bread       7
rjen
  • 1,938
  • 1
  • 7
  • 19