问题 I have data with 'from' and 'to' columns: df = data.frame(from = c('A','A','X','E','B','W','C','Y'), to = c('B','E','Y','C','A','X','A','W')) I'd like to identify all sequences of 'from-to', considering two or more rows, which starts and ends on the same value. An easy one would be A-B-A : # df # from to # 1 A B # 1. From A to B # 2 A E # 3 X Y # 4 E C # 5 B A # 2. From B and back to the starting point A, completing the sequence A-B-A # 6 W X # 7 C A # 8 Y W Another one: # df # from to # 1 A