I have
df <- data.frame(id = c(c(letters[1:4]), c(LETTERS[5:8])),
group = c(rep("same", length = 4), rep("opp", length = 4)),
match = c("H", "G", "E", "F", "c", "d", "b", "a"))
where each id (row) is uniquely paired with one other id in the list. Hoping to find tidyverse solution to create an indicator column showing unique pairings using sequential numbers assigned to each pair. So the result would be the output of
df <- data.frame(pair = c(1,2,3,4,3,4,2,1), id = c(c(letters[1:4]), c(LETTERS[5:8])),
group = c(rep("same", length = 4), rep("opp", length = 4)),
match = c("H", "G", "E", "F", "c", "d", "b", "a"))
In case of one to one relationship
df %>% group_by(pair = pmin(id, match))%>% mutate(pair = cur_group_id())
# A tibble: 8 × 4
# Groups: pair [4]
id group match pair
<chr> <chr> <chr> <int>
1 a same H 1
2 b same G 2
3 c same E 3
4 d same F 4
5 E opp c 3
6 F opp d 4
7 G opp b 2
8 H opp a 1
or
mutate(df, pair = match(pmin(id, match), id))
in Base R:
transform(df, pair = match(pmin(id,match), id))
id group match pair
1 a same H 1
2 b same G 2
3 c same E 3
4 d same F 4
5 E opp c 3
6 F opp d 4
7 G opp b 2
8 H opp a 1
In case of one to many relationship:
df %>%
group_by(pair= paste(pmin(id, match), pmax(id, match)))%>%
mutate(pair =cur_group_id())
# A tibble: 8 × 4
# Groups: pair [4]
id group match pair
<chr> <chr> <chr> <int>
1 a same H 1
2 b same G 2
3 c same E 3
4 d same F 4
5 E opp c 3
6 F opp d 4
7 G opp b 2
8 H opp a 1