0

I am looking for a solution to the following problem:

dp <- c('a','x','b','z','c')

rf <- c('b','z','c','a','a')

df <- data.frame(dp, rf)

For this example dataframe i want to recognize, if there are cycles from dp to rf. Such a cycle would be in this example: a --> b, b --> c, c --> a. In this case i would like to remove the row where dp = c and rf = a. The cycle can be longer than just over these 3 dependencies.

Thanks in advance for helping!

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
goegges
  • 107
  • 5
  • First, get the numerical value of the letter (or assign one if you work with something else). Then check if `dp[i+1] - dp[i] == 1`. Would something like that work? – mhovd Jul 09 '20 at 16:20
  • Not really sure how this can work. There can also be other cycles than in the example. Other then that your solution is only looking at ```dp``` but not ```rf```. Can you maybe elaborate your suggestion more in detail? – goegges Jul 09 '20 at 16:56
  • 1
    Can you add some more examples with expected output? – mhovd Jul 09 '20 at 16:59
  • 2
    Maybe load into an graph object using `igraph` and then [find all cycles](https://stackoverflow.com/questions/55091438/r-igraph-find-all-cycles) – MrFlick Jul 09 '20 at 18:03
  • 2
    See also [Find all existed cycles from data in R](https://stackoverflow.com/questions/59997320/find-all-existed-cycles-from-data-in-r) – Henrik Jul 09 '20 at 18:32

0 Answers0