For my dataset, I have two columns of data containing employee names and I am trying to the find the distance between two employees in the org's hierarchy. Here's a snippet of what the data looks like:
| Employee | Supervisor|
| -------- | --------- |
| Wald, Z | Holt, K |
| Kent, P | Wald, Z |
| Clark, O | Kent, P |
Is there any code I could write in R to find the distance between Clark, O
and Holt, K
?
CodePudding user response:
Assuming the input shown reproducibly in the Note at the end convert it to a matrix and then to an igraph object. Now use distances
to get the distances between every pair of vertices.
library(igraph)
g <- graph_from_edgelist(as.matrix(DF))
distances(g)
## Wald, Z Holt, K Kent, P Clark, O
## Wald, Z 0 1 1 2
## Holt, K 1 0 2 3
## Kent, P 1 2 0 1
## Clark, O 2 3 1 0
set.seed(123)
plot(g)
Note
Lines <- "Employee | Supervisor
Wald, Z | Holt, K
Kent, P | Wald, Z
Clark, O | Kent, P "
DF <- read.table(text = gsub("|", "", Lines), header = TRUE, sep = "|",
strip.white = TRUE)
CodePudding user response:
I don't have enough reputation to comment on the additional question above, but if you have a data frame/tibble you can do basically the same thing with the graph_from_data_frame
function.
library(igraph)
g = graph_from_data_frame(select(data, Employee, Supervisor))
distances(g)