回答編集履歴
2
test
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
mutate(group_id = list(which(colSums(c_across(everything()) == t(.)) > 0))) %>%
|
13
13
|
mutate(group_id =
|
14
14
|
reduce(.$group_id,
|
15
|
-
~{ s <- intersect(unlist(.x),
|
15
|
+
~{ s <- intersect(unlist(.x), .y); ifelse(length(s), list(s), .x) },
|
16
16
|
.init=list(group_id))) %>%
|
17
17
|
group_by(group_id) %>%
|
18
18
|
mutate(group_id = cur_group_id()) -> data
|
1
test
CHANGED
@@ -1,18 +1,26 @@
|
|
1
|
+
```r
|
1
|
-
|
2
|
+
suppressMessages(library(tidyverse))
|
2
3
|
|
3
|
-
|
4
|
+
data <- data.frame(
|
5
|
+
V1 = c(1, 1, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7),
|
6
|
+
V2 = c(1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 7),
|
7
|
+
V3 = c(1, 1, 1, 2, 3, 4, 5, 5, 6, 5, 6, 7, 8)
|
8
|
+
)
|
4
9
|
|
5
|
-
```r
|
6
10
|
data %>%
|
7
11
|
rowwise() %>%
|
8
12
|
mutate(group_id = list(which(colSums(c_across(everything()) == t(.)) > 0))) %>%
|
13
|
+
mutate(group_id =
|
14
|
+
reduce(.$group_id,
|
15
|
+
~{ s <- intersect(unlist(.x), unlist(.y)); ifelse(length(s), list(s), .x) },
|
16
|
+
.init=list(group_id))) %>%
|
9
17
|
group_by(group_id) %>%
|
10
18
|
mutate(group_id = cur_group_id()) -> data
|
11
19
|
|
12
20
|
data
|
13
21
|
|
14
22
|
# # A tibble: 13 × 4
|
15
|
-
# # Groups: group_id [
|
23
|
+
# # Groups: group_id [5]
|
16
24
|
# V1 V2 V3 group_id
|
17
25
|
# <dbl> <dbl> <dbl> <int>
|
18
26
|
# 1 1 1 1 1
|
@@ -21,12 +29,11 @@
|
|
21
29
|
# 4 3 3 2 2
|
22
30
|
# 5 3 3 3 2
|
23
31
|
# 6 4 4 4 3
|
24
|
-
# 7 4 4 5
|
32
|
+
# 7 4 4 5 3
|
25
|
-
# 8 4 5 5
|
33
|
+
# 8 4 5 5 3
|
26
|
-
# 9 4 5 6
|
34
|
+
# 9 4 5 6 3
|
27
|
-
# 10 5 5 5
|
35
|
+
# 10 5 5 5 3
|
28
|
-
# 11 5 5 6
|
36
|
+
# 11 5 5 6 3
|
29
|
-
# 12 6 6 7
|
37
|
+
# 12 6 6 7 4
|
30
|
-
# 13 7 7 8
|
38
|
+
# 13 7 7 8 5
|
31
39
|
```
|
32
|
-
|