我知道这比R相关更算法,但我想知道解决方案是否存在于任何存在的R包中。此代码示例标识作为VP的员工的第一个经理。有没有更快的方法在大量数据上执行此操作?这似乎是深度优先搜索。可以使用graph.dfs
完成吗?
x <- data.frame(employee=c('xy', 'abc', 'zz', 'xx', 'yy', 'ww'), manager=c('abc', 'zz', 'xx', 'yy', 'ww', 'uu'), level=c('dir', 'man', 'vp', 'vp', 'man', 'vp'))
emps <- as.character(unique(x$employee))
x$employee<-as.character(x$employee)
x$manager<-as.character(x$manager)
findVP <- function(emp){
employee <- x[which(x$employee == emp),]
if(employee$level == 'vp'){
return (emp)
} else{
findVP(x[which(x$employee == emp),]$manager)
}
}
sapply(emps, findVP)
预期产出(如果员工是副总裁,应自行退货):
emp first_VP_manager
"xy" "zz"
"abc" "zz"
"zz" "zz"
"xx" "xx"
"yy" "ww"
"ww" "ww"
答案 0 :(得分:1)
我在igraph
没有专业人士,但是如果你想使用图形数据结构,你可以做一些类似的事情。
## Setup an edgelist
vps <- x[x$level=='vp', 'employee']
mat <- rbind(as.matrix(x[!(x$employee %in% vps),2:1]), cbind('vp', vps))
## Make a graph and look at it
library(igraph)
g <- graph_from_edgelist(mat)
plot(g, layout=layout.reingold.tilford(g, root="vp"))
## Leaves of graph (thought there would be a function for this?)
leaves <- V(g)[degree(g, mode='out')==0]
## Get the vps for each branch, print result
res <- lapply(all_simple_paths(g, from='vp', to=leaves), function(x) names(x)[-1])
setNames(res, sapply(res, `[`, 1))
# $zz
# [1] "zz" "abc" "xy"
#
# $ww
# [1] "ww" "yy"
#
# $xx
# [1] "xx"