I have a dataframe in R that I need to reshape to show paired relationships (basically building a version of two-column adjacency list - except it's a bit more complex than that). Here's the dataframe.
up_1 <- c(1, 1, 1, 1, 1, 1)
up_2 <- c(4, 3, 2, 3, 3, 3)
up_3 <- c(NA, 7, 5, 6, 6, 6)
up_4 <- c(NA, NA, 8, 9, 10, 11)
df_wide <- tibble(up_1, up_2, up_3, up_4)
df_wide
up_1 up_2 up_3 up_4
<dbl> <dbl> <dbl> <dbl>
1 4 NA NA
1 3 7 NA
1 2 5 8
1 3 6 9
1 3 6 10
1 3 6 11
The dataframe represents ordered paths through a tree from root (1) to leaves (4, 7, 8, 9, 10, 11). Note that this is an example dataframe - the eventual application will use a much larger dataframe with variable dimensions and multiple roots.
Here is the actual example tree, in illustration:
1
├─ 2
├─ 5
├─ 8
├─ 3
├─ 6
├─ 9
├─ 10
├─ 11
├─ 7
├─ 4
I need to transform/pivot/spread this to two columns where the left column lists all nodes and the right column lists all "up-network" nodes, long format. So it would end up looking like this:
down up
<dbl> <dbl>
1 4
1 3
1 7
1 6
1 9
1 10
1 11
1 2
1 5
1 8
4 NA
3 7
3 6
3 9
3 10
3 11
7 NA
6 9
6 10
6 11
9 NA
10 NA
11 NA
2 5
2 8
5 8
8 NA
In this example, there are 10 values "up-network" of 1 and 5 values "up-network" of 3.
Clarifying the application: grouping by the down column, a person could identify all up values up-network for any given down value (and from there incorporate and summarize other relevant attributes associated with up values).
I've had a hard time figuring this out and welcome any insight from all the stackoverflow heroes out there! Thank you in advance.
Post-script - I know this would be easier in a more easily object-oriented language (e.g. Python), but for now I'm stuck in R, given the broader analytic context.