压平不平衡(参差不齐)的层次结构

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

所以我有一个 .csv 文件,它垂直显示参差不齐的层次结构(左侧的表格)。 indent_nbr 表示每个项目在层次结构中所处的级别,其中 0 是顶级父级。

我想使用 R 代码将这个层次结构展平,使其看起来像右侧的表格。有人可以帮我吗?

请注意,我仅限于使用以下软件包: base、boot、class、cluster、codetools、编译器数据集、foreign、graphics、grDevices、grid、Kernsmooth、lattice、MASS、Matrix、methods、mgcv、nlme、nnet、parallel、rpart、spatial、splines、stats、stats4、survival 、tcltk、工具、翻译、utils

非常感谢您的帮助

r pivot hierarchy flatten ragged
1个回答
0
投票

你可以尝试这个,它使用了 Ruben

repeat_last
。所以不需要额外的包。

> g <- c(0, cumsum(diff(dat$indent_nbr) != 1))
> a <- array(dim=c(length(table(g)), length(table(dat$indent_nbr))))
> a[cbind(g + 1, dat$indent_nbr + 1)] <- dat$item
> na <- apply(!is.na(a), 1, \(x) max(cumsum(diff(x) >= 0) + 1)) + 1
> a <- apply(a, 2, repeat_last)
> w <- which(na <= ncol(a))
> a[t(mapply(cbind, w, lapply(na[w], `:`, ncol(a))))] <- NA
> a
      [,1] [,2] [,3] [,4] [,5] [,6]
 [1,] "A"  "B"  "C"  "D"  "E"  NA  
 [2,] "A"  "B"  "C"  "D"  "F"  NA  
 [3,] "A"  "B"  "C"  "D"  "G"  "H" 
 [4,] "A"  "B"  "C"  "D"  "G"  "I" 
 [5,] "A"  "B"  "C"  "D"  "G"  "J" 
 [6,] "A"  "B"  "C"  "D"  "G"  "K" 
 [7,] "A"  "B"  "C"  "D"  "L"  "M" 
 [8,] "A"  "B"  "C"  "D"  "L"  "N" 
 [9,] "A"  "B"  "C"  "D"  "L"  "O" 
[10,] "A"  "B"  "C"  "D"  "L"  "P" 
[11,] "A"  "B"  "C"  "Q"  "R"  NA  

数据:

> dput(dat)
structure(list(item = c("A", "B", "C", "D", "E", "F", "G", "H", 
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R"), indent_nbr = c(0, 
1, 2, 3, 4, 4, 4, 5, 5, 5, 5, 4, 5, 5, 5, 5, 3, 4)), class = "data.frame", row.names = c(NA, 
-18L))
© www.soinside.com 2019 - 2024. All rights reserved.