Hi, everyone,
I am looking for a way to transform a table like the "parent_child" table into a table whose columns correspond to the different levels found in the previous table. Basically, I would like to turn 'parent_child' into 'wanted_table'.
parent_child <- data.frame(
child = c("Paris", "Lyon","France", "Tokyo", "Japan", "NewYork", "USA", "Casablanca", "Morocco", "Beijing", "China"),
parent = c("France", "France", "Europe", "Japan", "Asia", "USA", "America", "Morocco", "Africa", "China", "Asia")
)
wanted_table <- data.frame(
level_1 = c("Europe", "Europe", "Asia", "Asia", "America", "Africa"),
level_2 = c("France", "France", "Japan", "China", "USA", "Morocco"),
level_3 = c("Paris", "Lyon", "Tokyo", "Beijing", "NewYork", "Casablanca")
)
That is:
Turn this:
Into this:
Any help would be highly appreciated.
Many thanks.