0

I have a data set similar to the following:

ID   Parent_ID    Outcome
1a                 Black
2a                 Green
3a       1a        Red
4a                 Blue
5a       2a        Yellow
6a       5a        Orange
7a                 Teal
8a                 Purple
9a       3a        Brown

with Parent_IDs linked to a previous event, and which also have their own IDs generated each time an event occurs. What I'd like to do is group together related events in the form of subsequent rows, or by creating one data frame with multiple columns that link each event.

Output re: option 1 (rows):

ID   Parent_ID    Outcome
1a                 Black
3a       1a        Red
9a       3a        Brown
2a                 Green
5a       2a        Yellow
6a       5a        Orange
4a                 Blue
7a                 Teal
8a                 Purple

Output re: option 2 (data frame):

ID   Parent_ID    Outcome 1   ID_2    Outcome 2   ID_3     Outcome_3
1a                 Black       3a       Red        9a        Brown
2a                 Green       5a       Yellow     6a        Orange
4a                 Blue        NA       NA         NA        NA
7a                 Teal        NA       NA         NA        NA
8a                 Purple      NA       NA         NA        NA

I've tried sub-setting blank Parent_ID, creating a new data frame and binding the resulting rows but I don't think this would solve my issue beyond 2 instances of an event. My issue is handling the fact that there could be (potentially) an infinite number of related events. Does anyone have any suggestions? Thank you for your time.

the_witch_dr
  • 123
  • 1
  • 9
  • 1
    Did you see this? https://stackoverflow.com/questions/33069353/r-hierarchical-data-frame-from-child-parent-relations – user2474226 Nov 04 '19 at 17:31
  • @user2474226 I haven't thank you I will take a peek. The solutions provided here appear to do exactly what I need. – the_witch_dr Nov 04 '19 at 17:34

0 Answers0