Given the following tibble
tibble(sample = c(1:6),
string = c("ABC","ABC","CBA","FED","DEF","DEF"),
x = c("a","a","b","e","d","d"),
y = c("b","b","a","d","e","e"))
# A tibble: 6 × 4
sample string x y
<int> <chr> <chr> <chr>
1 1 ABC a b
2 2 ABC a b
3 3 CBA b a
4 4 FED e d
5 5 DEF d e
6 6 DEF d e
I would like to group rows by the unordered combination of columns x,y
, then flip x
⇔ y
and reverse string
in instances where x,y
is inverted relative to the first row in the group. The desired output:
# A tibble: 6 × 5
sample string x y group
<int> <chr> <chr> <chr> <dbl>
1 1 ABC a b 1
2 2 ABC a b 1
3 3 ABC a b 1
4 4 FED e d 2
5 5 FED e d 2
6 6 FED e d 2