我已经汇总了我试图以“整洁”格式获得的数据。这是一个数据示例。1级=同一组中所有2级或3级项目的总和。
光源(2)是电气(1)的子构件。配线(3)和灯泡(3)是光源(2)的子组件,依此类推。
200电气= 82盏灯+ 118个通风口
或者可以将其分解为:
200电气= 60根电线+ 22个灯泡+ 60个探测器+ 20根电缆+ 25根塑料+ 13个支架
Level Group Name Price
1 01 Electrical 200
2 01 Lights 82
3 01 Wiring 60
3 01 Bulbs 22
2 01 Vent 118
3 01 Detector 60
3 01 Cable 20
3 01 Plastic 25
3 01 Brackets 13
1 02 Interior 500
2 02 Doors 300
3 02 Knobs 40
2 02 Trim 200
3 02 Shelves 150
4 02 Brackets 50 然后,对组02 (内部)重复该过程。我正在尝试以一种整洁/更可行的格式获得这些数据,这样我就可以做一些分析,而不需要重复或三次计算每件商品的价格。电气(200)和内部(500)的总和应为700。但是当我对我的Price列求和时,我们得到1840,因为项目被多次计算。
发布于 2021-05-26 04:18:07
不是tidyverse,但数据可以转换为data.tree和图形格式,这些格式具有处理树和图形的功能,也可以用于转换为其他格式,例如as.list( nn ),其中nn是下面定义的data.tree节点对象是嵌套列表表示。有关可复制形式的输入数据,请参阅末尾的注释。
library(data.tree)
library(sqldf)
library(yaml)
# compute parents
DF2 <- sqldf("select a.*, max(b.rowid) parent
from DF a
left join DF b on b.Level = a.Level - 1 and b.rowid < a.rowid
group by a.rowid")
# leaves are those rows that are not a parent
leaves <- setdiff(1:nrow(DF2), DF2$parent)
# format as a yaml string and then read the yaml into a data tree node structure
s <- sapply(1:nrow(DF2), function(i) with(DF2[i, ], paste(
if (Level == 1) paste0("Group-", Group, ":\n"),
strrep(" ", Level),
paste0(i, Name),
":",
if (i %in% leaves) paste("\n", strrep(" ", Level), " Price:", Price)
)))
y <- yaml.load(unlist(strsplit(s, "\n")))
nn <- as.Node(y)
print(nn, "Price")给予(输出后继续)
levelName Price
1 Root NA
2 ¦--Group-1 NA
3 ¦ °--1Electrical NA
4 ¦ ¦--2Lights NA
5 ¦ ¦ ¦--3Wiring 60
6 ¦ ¦ °--4Bulbs 22
7 ¦ °--5Vent NA
8 ¦ ¦--6Detector 60
9 ¦ ¦--7Cable 20
10 ¦ ¦--8Plastic 25
11 ¦ °--9Brackets 13
12 °--Group-2 NA
13 °--10Interior NA
14 ¦--11Doors NA
15 ¦ °--12Knobs 40
16 °--13Trim NA
17 °--14Shelves NA
18 °--15Brackets 50我们也可以转换成一个字形对象。
library(igraph)
set.seed(123)
g <- as.igraph(nn)
plot(g, vertex.shape = "none", vertex.label.cex = 0.7)

备注
Lines <- "Level Group Name Price
1 01 Electrical 200
2 01 Lights 82
3 01 Wiring 60
3 01 Bulbs 22
2 01 Vent 118
3 01 Detector 60
3 01 Cable 20
3 01 Plastic 25
3 01 Brackets 13
1 02 Interior 500
2 02 Doors 300
3 02 Knobs 40
2 02 Trim 200
3 02 Shelves 150
4 02 Brackets 50"
DF <- read.table(text = Lines, header = TRUE)发布于 2021-05-26 03:33:28
一种data.table方法
像split shou;这样的东西可以帮你入门。
示例数据
library(data.table)
DT <- fread("Level Group Name Price
1 01 Electrical 200
2 01 Lights 82
3 01 Wiring 60
3 01 Bulbs 22
2 01 Vent 118
3 01 Detector 60
3 01 Cable 20
3 01 Plastic 25
3 01 Brackets 13
1 02 Interior 500
2 02 Doors 300
3 02 Knobs 40
2 02 Trim 200
3 02 Shelves 150
4 02 Brackets 50 ")代码
split(DT, by = c("Group", "Level"))输出(列表)
$`1.1`
Level Group Name Price
1: 1 1 Electrical 200
$`1.2`
Level Group Name Price
1: 2 1 Lights 82
2: 2 1 Vent 118
$`1.3`
Level Group Name Price
1: 3 1 Wiring 60
2: 3 1 Bulbs 22
3: 3 1 Detector 60
4: 3 1 Cable 20
5: 3 1 Plastic 25
6: 3 1 Brackets 13
$`2.1`
Level Group Name Price
1: 1 2 Interior 500
$`2.2`
Level Group Name Price
1: 2 2 Doors 300
2: 2 2 Trim 200
$`2.3`
Level Group Name Price
1: 3 2 Knobs 40
2: 3 2 Shelves 150
$`2.4`
Level Group Name Price
1: 4 2 Brackets 50https://stackoverflow.com/questions/67693579
复制相似问题