需要递归地拉动父母创建树r的帮助

发布于 2025-02-01 21:40:09 字数 1082 浏览 2 评论 0原文

我有一个看起来像这样的数据框架:

名字经理
约翰·比尔
·比尔·戴维
·戴维·莎拉·
莎拉(John Bill David David Sarah
name = c("John","Bill","David","Sarah")
manager = c("Bill","David","Sarah","")
df = data.frame(name,manager)

)(莎拉(Sarah)是梯子的顶级),我只是想做一个简单的查找来吸引约翰的经理(大卫)。 Ideally I would like a recursive loop in R that will run through my dataset and create an output that lists all the managers above each person such as this:

namemanageroutput
JohnBillBill_David_Sarah
BillDavidDavid_Sarah
DavidSarahSarah
Sarah

Also please note, my data没有以这种方式订购,经理可以管理多个人,而不是一对一。任何帮助将不胜感激!

I have a dataframe that looks like this:

namemanager
JohnBill
BillDavid
DavidSarah
Sarah
name = c("John","Bill","David","Sarah")
manager = c("Bill","David","Sarah","")
df = data.frame(name,manager)

(Sarah is the top of the ladder) I'm just trying to do a simple lookup to pull in John's manager's manager (David). Ideally I would like a recursive loop in R that will run through my dataset and create an output that lists all the managers above each person such as this:

namemanageroutput
JohnBillBill_David_Sarah
BillDavidDavid_Sarah
DavidSarahSarah
Sarah

Also please note, my data is not ordered in such a manner, and a manager can manage multiple people, its not one to one. Any help would be appreciated thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望喜 2025-02-08 21:40:09

这是一个具有igraph的解决方案。从data.frame创建有向图,然后lapply loops提取所需的信息。

name = c("John","Bill","David","Sarah")
manager = c("Bill","David","Sarah","")
df = data.frame(name,manager)

suppressPackageStartupMessages(library(igraph))

g <- graph_from_data_frame(df)
plot(g)

“”

ll <- lapply(V(g), \(v) all_simple_paths(g, from = v, to = V(g)[V(g) != v]))
ll <- lapply(ll, \(l) l[length(l)])
df$output <- sapply(ll[-length(ll)], \(l) {
  if(length(l) > 0) {
    inx <- eval(parse(text = l))[-1]
    nms <- names(ll)[ V(g)[inx] ]
    paste(nms[nms != ""], collapse = "_")
  } else ""
})

df
#>    name manager           output
#> 1  John    Bill Bill_David_Sarah
#> 2  Bill   David      David_Sarah
#> 3 David   Sarah            Sarah
#> 4 Sarah

reprex软件包(v2.0.1)

Here is a solution with igraph. Create a directed graph from the data.frame, then a sequence of lapply loops extracts the information needed.

name = c("John","Bill","David","Sarah")
manager = c("Bill","David","Sarah","")
df = data.frame(name,manager)

suppressPackageStartupMessages(library(igraph))

g <- graph_from_data_frame(df)
plot(g)

ll <- lapply(V(g), \(v) all_simple_paths(g, from = v, to = V(g)[V(g) != v]))
ll <- lapply(ll, \(l) l[length(l)])
df$output <- sapply(ll[-length(ll)], \(l) {
  if(length(l) > 0) {
    inx <- eval(parse(text = l))[-1]
    nms <- names(ll)[ V(g)[inx] ]
    paste(nms[nms != ""], collapse = "_")
  } else ""
})

df
#>    name manager           output
#> 1  John    Bill Bill_David_Sarah
#> 2  Bill   David      David_Sarah
#> 3 David   Sarah            Sarah
#> 4 Sarah

Created on 2022-05-27 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文