R Tidyverse 递归函数路径创建问题

问题描述 投票:0回答:1

在物料清单的上下文中,我有一个树结构,我想执行递归函数来查找该树的不同路径。 然而,在尝试定义子装配体共享的材料路径时,我犯了一个错误。我应该怎么做才能解决这个问题?请帮忙!

这是我的 bom 结构:

BOM <- tibble(
bomID = 1:24,
finiGoodID = c(rep(1, 12), rep(2, 12)), 
matID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 
          11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 5, 6),
parentMatID = c(NA, 1, 2, 2, 4, 4, 4, 4, 4, 1, 3, 3, 
                NA, 11, 12, 12, 14, 14, 14, 14, 14, 11, 13, 13))

现在的功能是:

bomPath <- function(mat, fg) {
row <- subset(BOM, matID == mat & finiGoodID == fg)
    if (nrow(row) == 0) {
    return(mat)
}
    parent = row$parentMatID[1]  

if (is.na(parent)) {
    return(mat)
} else {
    # Recursively build the path
    parent_path = Recall(parent, fg)
    return(c(mat, parent_path))
}
}

我这样称呼这个函数:

for (i in 1:nrow(BOM)) {
path = bomPath(BOM$matID[i], BOM$finiGoodID[i])
print(path)
}

我想创建一些简单的东西,但结果显示如下:

[1] 1
[1] 2 1
[1] 3 2 1
[1] 4 2 1
[1] 5 4 2 1
[1] 6 4 2 1
[1] 7 4 2 1
[1] 8 4 2 1
[1] 9 4 2 1
[1] 10  1
[1] 5 4 2 1 #This was supposed to be [1] 5 3 2 1
[1] 6 4 2 1 #This was supposed to be [1] 6 3 2 1
[1] 11
[1] 12 11
[1] 13 12 11
[1] 14 12 11
[1] 15 14 12 11
[1] 16 14 12 11
[1] 17 14 12 11
[1] 18 14 12 11
[1] 19 14 12 11
[1] 20 11
[1]  5 13 12 11
[1]  6 13 12 11

知道我哪里搞砸了吗?或者如何解决这个问题?情况紧急请帮忙。我将根据这个路径定义进行计算。

谢谢!

r path tidyverse byte-order-mark recursive-datastructures
1个回答
0
投票

这是一个解决方案

igraph

dfs <- with(BOM, split(data.frame(matID, parentMatID), finiGoodID))
unlist(
    lapply(
        dfs,
        \(x) {
            g <- transform(
                x,
                parentMatID = ifelse(is.na(parentMatID), matID, parentMatID)
            ) %>%
                graph_from_data_frame() %>%
                simplify()
            src <- V(g)[degree(g, mode = "out") == 0]
            lapply(
                shortest_paths(g, src, as.character(x$matID), mode = "in")$vpath,
                \(p) {
                    as.integer(names(rev(p)))
                }
            )
        }
    ),
    FALSE,
    FALSE
)

这可以给

[[1]]
[1] 1

[[2]]
[1] 2 1

[[3]]
[1] 3 2 1

[[4]]
[1] 4 2 1

[[5]]
[1] 5 3 2 1

[[6]]
[1] 6 3 2 1

[[7]]
[1] 7 4 2 1

[[8]]
[1] 8 4 2 1

[[9]]
[1] 9 4 2 1

[[10]]
[1] 10  1

[[11]]
[1] 5 3 2 1

[[12]]
[1] 6 3 2 1

[[13]]
[1] 11

[[14]]
[1] 12 11

[[15]]
[1] 13 12 11

[[16]]
[1] 14 12 11

[[17]]
[1] 15 14 12 11

[[18]]
[1] 16 14 12 11

[[19]]
[1] 17 14 12 11

[[20]]
[1] 18 14 12 11

[[21]]
[1] 19 14 12 11

[[22]]
[1] 20 11

[[23]]
[1]  5 13 12 11

[[24]]
[1]  6 13 12 11
© www.soinside.com 2019 - 2024. All rights reserved.