需要递归地拉动父母创建树r的帮助
我有一个看起来像这样的数据框架:
名字 | 经理 |
---|---|
约翰· | 比尔 |
·比尔 | ·戴维 |
·戴维 | ·莎拉· |
莎拉(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:
name | manager | output |
---|---|---|
John | Bill | Bill_David_Sarah |
Bill | David | David_Sarah |
David | Sarah | Sarah |
Sarah |
Also please note, my data没有以这种方式订购,经理可以管理多个人,而不是一对一。任何帮助将不胜感激!
I have a dataframe that looks like this:
name | manager |
---|---|
John | Bill |
Bill | David |
David | Sarah |
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:
name | manager | output |
---|---|---|
John | Bill | Bill_David_Sarah |
Bill | David | David_Sarah |
David | Sarah | Sarah |
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个具有
igraph
的解决方案。从data.frame创建有向图,然后lapply
loops提取所需的信息。由 reprex软件包(v2.0.1)
Here is a solution with
igraph
. Create a directed graph from the data.frame, then a sequence oflapply
loops extracts the information needed.Created on 2022-05-27 by the reprex package (v2.0.1)