tidygraph

How to construct an edgeliste from a list of visited places (effectively)?

邮差的信 提交于 2020-05-15 09:35:11
问题 My original data.table consists of three columns. site , observation_number and id . E.g. the following which is all the observations for id = z |site|observation_number|id |a | 1| z |b | 2| z |c | 3| z Which means that ID z has traveled from a to b to c . There is no fixed number of sites per id. I wish to transform the data to an edge list like this |from |to||id| |a | b| z | |b | c| z | mock data sox <- data.table(site = c('a','b','c','a','c','c','a','d','e'), obsnum =c(1,2,3,1,2,1,2,3,4),

Mutate value by using a value from a different row in a tibble

夙愿已清 提交于 2019-12-11 00:32:52
问题 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):