我正在尝试创建一个员工表,他们向谁报告以及他们是什么级别。
我一直在使用类似的表,@ TonakShah足够帮助我计算最低级别的位置,并且上面的级别正在使用下面的解决方案。
我的员工表如下:
input = structure(list(Level.1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = "Board", class = "factor"), Level.2 = structure(c(2L,
2L, 2L, 1L, 1L, 3L, 3L), .Label = c("Aasha", "Grace", "Marisol"
), class = "factor"), Level.3 = structure(c(4L, 4L, 3L, 1L, 1L,
2L, 2L), .Label = c("Alex", "Chandler", "Millie", "Tushad"), class = "factor"),
Level.4 = structure(c(2L, 2L, 6L, 1L, 5L, 3L, 4L), .Label = c("#",
"Frank", "Joey", "Rachel", "Sarah", "Tony"), class = "factor"),
Level.5 = structure(c(3L, 2L, 1L, 1L, 1L, 4L, 1L), .Label = c("#",
"Lela", "Millie", "Ross"), class = "factor"), Level.6 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = "#", class = "factor")), class = "data.frame", row.names = c(NA,
-7L))
并使用Ronak在此处描述的技术(stackoverflow.com/questions/56903188/create-a-table-from-a-hierarchy/)
as.data.frame(t(apply(input, 1, function(x)
{new_x = x[x != "###"]; c(rev(tail(new_x, 2)), length(new_x)) })))
我可以获得所需的大部分表格。但是我很难找到“老板”(例如那些有雇员但没有“董事会”的人)。
我的理想输出看起来像这样(我添加了colnames以便于理解):
structure(list(Subordinate = structure(c(9L, 4L, 14L, 5L, 7L,
13L, 9L, 2L, 1L, 12L, 11L, 6L, 3L, 8L, 10L), .Label = c("Aasha",
"Alex", "Chandler", "Frank", "Grace", "Joey", "Lela", "Marisol",
"Millie", "Rachel", "Ross", "Sarah", "Tony", "Tushad"), class = "factor"),
Boss = structure(c(5L, 10L, 6L, 3L, 5L, 9L, 6L, 1L, 3L, 2L,
7L, 4L, 8L, 3L, 4L), .Label = c("Aasha", "Alex", "Board",
"Chandler", "Frank", "Grace", "Joey", "Marisol", "Millie",
"Tushad"), class = "factor"), Level = c(5L, 4L, 3L, 2L, 5L,
4L, 3L, 3L, 2L, 4L, 5L, 4L, 3L, 2L, 4L)), class = "data.frame", row.names = c(NA,
-15L))
我想我可能会循环执行,但这似乎不是最好的答案。谁能提供其他提示?
答案 0 :(得分:1)
无法提出一个更漂亮的解决方案,但这可行。在先前使用的while
调用中使用apply
循环,我们可以完成
output <- do.call(rbind.data.frame, apply(input, 1, function(x) {
new_x = as.character(x[x != "#"])
list_df <- list()
i = 1
while(length(new_x) >= 2) {
#Get last 2 eneteries
list_df[[i]] <- c(rev(tail(new_x, 2)), length(new_x))
#Go one level deeper
new_x = head(new_x, -1)
i = i +1
}
do.call(rbind, list_df)
}))
#To remove duplicate enteries
output[!duplicated(output), ]
# V1 V2 V3
#1 Millie Frank 5
#2 Frank Tushad 4
#3 Tushad Grace 3
#4 Grace Board 2
#5 Lela Frank 5
#9 Tony Millie 4
#10 Millie Grace 3
#12 Alex Aasha 3
#13 Aasha Board 2
#14 Sarah Alex 4
#17 Ross Joey 5
#18 Joey Chandler 4
#19 Chandler Marisol 3
#20 Marisol Board 2
#21 Rachel Chandler 4