Hey everybody, I have been running this through my head multiple times for the past few days and cannot come up with an elegant/tidy conclusion. I have two columns of IDs. Example data:
df <- data.frame(pid = c(1,2,3,5),
cid = c(3,4,5,6))
df
#> pid cid
#> 1 1 3
#> 2 2 4
#> 3 3 5
#> 4 5 6
wanted_df <- data.frame(pid = c(1, 1, 1, 2, 3, 3, 5),
cid = c(3, 5, 6, 4, 5, 6, 6))
wanted_df
#> pid cid
#> 1 1 3
#> 2 1 5
#> 3 1 6
#> 4 2 4
#> 5 3 5
#> 6 3 6
#> 7 5 6
Created on 2019-02-24 by the reprex package (v0.2.1)
Every child id can be a parent id. The output should be two columns, where the pid can be a chain of all of its children. From the example, we can see 1 relates to 3 which relates to 5, etc. Those pairs should be their own row. The length of relationships can vary from dataset to dataset.
Are recursive functions best for this? I was trying to part with my ugly while loop for a possible purrr/map way.
Any help and/or direction in the best path for this is greatly appreciated!
Thanks,
Kyle