I want to calculate the distance a node to the root dtr
. All I have is a vector, that contains the parent node id for each node rel
(in this example id == 7
is root):
library(tidyverse)
tmp <- tibble(
id = 1:12,
rel = c(2,7,4,2,4,5,7,7,10,8,7,7)
)
In the end I'm looking for this result:
tmp$dtr
[1] 2 1 3 2 3 4 0 1 3 2 1 1
So far I was able to write the following algorithm until I got stuck when trying to reference a different row in my code.
The algorithm should work like this (Pseudocode):
- If not root, increment
dtr
:if(!equals(tid,trel)): dtr = dtr+1
- Change
tid
totrel
:tid = trel
- Change
trel
to to therel
value whereid == trel
- If any
!equals(tid,trel)
GOTO 1., else END
First I added 2 helper columns to store temporary information:
tmp <- tmp %>%
mutate(
tid = id,
trel = rel,
dtr = 0
)
The first two steps in the algorithm work like this:
tmp <- tmp %>%
mutate(
dtr = if_else(
!equals(tid,trel),
dtr + 1,
dtr
),
tid = trel
)
The 3rd step I'm not sure about.... I tried to achieve it with the following code, but that does not work:
tmp <- tmp %>%
mutate(trel = rel[id == .$tid])
The result is (of course) wrong:
tmp$rel
[1] 7 7 7 7 7 7 7 7 7 7 7 7
But why not this? (Should be the right solution when running 3. the first time):
[1] 2 7 2 7 2 4 7 7 10 8 7 7
The 4th step is done by checking if I have more than one unique value in trel:
while(length(unique(tmp$trel)) > 1){
...
}
Thus the full algorithm should somewhat look like this:
get_dtr <- function(tib){
tmp <- tib %>%
mutate(
tid = id,
trel = rel,
dtr = 0
)
while(length(unique(tmp$trel)) > 1){
tmp <- tmp %>%
mutate(
dtr = if_else(
!equals(tid,trel),
dtr + 1,
dtr
),
tid = trel
)
### Step 3
}
tmp
}
Any idea how to solve this or a simpler solution? Thanks in advance!