我不想重塑它,因为我有很多数据,所以像循环这样的东西会自动翻译它 输入-数据框1
Item LC ToLC
8T4121 MW92 WK14
8T4121 WK14 RM11
8T4121 WK14 RS11
8T4121 RS11 OY01
AB7651 MW92 RS11
AB7651 RS11 OY01
我想做一个循环,我可以得到这样的输出 数据框2
Item LC1 LC2 LC3 LC4
8T4121 MW92 WK14 RM11
8T4121 MW92 WK14 RS11 OY01
AB7651 MW92 RS11 OY01
我尝试过这样的事情:
bodlane <- lctolc
colnames(bodlane) <- c("Item","Entry","From")
bodlane$To <- lctolc$To[match(bodlane$From, lctolc$From)]
colnames(bodlane) <- c("Item","Entry","Parent","From")
bodlane$To <- lctolc$To[match(bodlane$From, lctolc$From)]
colnames(bodlane) <- c("Item","Entry","Parent","Parent1","From")
bodlane$To <- lctolc$To[match(bodlane$From, lctolc$From)]
colnames(bodlane) <- c("Item","LC","ToLC","Parent1","From","To")
答案 0 :(得分:0)
我相信可以使用igraph
来解决此问题,而方法与“recursive” self join in data.table类似,但无需计算。
这里的困难是每个Item
都有单独的图形。我的方法是将数据框拆分为图表列表。可能会有更简洁的解决方案使用type
顶点属性。
但是,下面的代码创建了预期的结果:
library(igraph)
library(data.table)
library(magrittr)
lapply(
lapply(split(lctolc, lctolc$Item), function(x) graph.data.frame(x[, 2:3])),
function(x) lapply(
all_simple_paths(x, from = V(x)[degree(x, mode = "in") == 0],
to = V(x)[degree(x, mode = "out") == 0]),
function(y) as.data.table(t(names(y))) %>% setnames(paste0("LC", seq_along(.)))
) %>% rbindlist(fill = TRUE)
) %>% rbindlist(fill = TRUE, idcol = "Item")
Item LC1 LC2 LC3 LC4 1: 8T4121 MW92 WK14 RS11 OY01 2: 8T4121 MW92 WK14 RM11 <NA> 3: AB7651 MW92 RS11 OY01 <NA>
待完成
library(data.table)
lctolc <- fread("
Item LC ToLC
8T4121 MW92 WK14
8T4121 WK14 RM11
8T4121 WK14 RS11
8T4121 RS11 OY01
AB7651 MW92 RS11
AB7651 RS11 OY01",
data.table = FALSE)