I have a table containing a hierarchical structure of some categories:
for example
"cat" belongs to "domestic animals", and "domestic animals" belong to "animals"
below I show my data with segment.parent that tells the macro-category to which each entity belongs to. I want to create a new table where I only have the leaves of the hierarchy and a variable for the two macro categories it belongs to
segment.id Name. segment.parent
1 cat 3
2 dog 3
3 domestic animals 4
4 animals NA
5 cake 7
6 ice-cream 7
7 dessert 8
8 food NA
9 main-course 8
what I want to obtain is the following
segment.id Name segment.parent Name.parent Name.parent.parent
1 cat 3 domestic animals animals
2 dog 3 domestic animals animals
5 cake 7 dessert food
6 ice-cream 7 dessert food