这是我的树:
library(data.tree)
data <- data.frame(pathString = c("Main",
"Main/1 Blue",
"Main/2 Red",
"Main/3 Others",
"Main/4 Others/6 Yellow",
"Main/5 Others/7 White"),
Value = c(10,3,2,5,2,3))
tree <- as.Node(data, Value)
print(tree, "Value")
如何在dataframe中编码叶节点ID(1、2、3、6和7)?例如,见下文。
Product ID Color Leaf_node
a Blue 1
b Red 2
c Yellow 6
d White 7
e Pink 3
f Blue 1
是否有一个有效的方法来实现这一点?
例2:
data <- data.frame(pathString = c("0",
"0/1",
"0/1/3",
"0/1/4",
"0/2",
"0/2/5",
"0/2/5/8",
"0/2/5/9",
"0/2/6",
"0/2/7"))
data$field <- c(' ', 'packtype', 'diet', 'diet', 'packtype', 'flavor', 'diet', 'diet', 'flavor', 'flavor')
data$value <- c(' ', 'Pack', 'N', 'Y', 'Carton', 'Classic', 'N', 'Y', 'Rich', 'Mellow')
tree <- as.Node(data, field, value)
print(tree, "field" ,"value")
levelName field value
1 0
2 ¦--1 packtype Pack
3 ¦ ¦--3 diet N
4 ¦ °--4 diet Y
5 °--2 packtype Carton
6 ¦--5 flavor Classic
7 ¦ ¦--8 diet N
8 ¦ °--9 diet Y
9 ¦--6 flavor Rich
10 °--7 flavor Mellow
期望输出:分配leaf_node到独立的data frame:variables: packtype, diet and flavor。
发布于 2018-07-25 09:54:09
tree$leaves
是一个列表,你可以用它names
。棘手的部分是过滤掉其他信息
foo <- grep("Other",
sub(".*\\.", "", names(tree$leaves)), invert = TRUE, value = TRUE)
data.frame(Color = sub(".* ", "", foo),
Leaf_node = as.numeric(sub(" .*", "", foo)))
Color Leaf_node
1 Blue 1
2 Red 2
3 Yellow 6
4 White 7
https://stackoverflow.com/questions/-100005699
复制相似问题