我有以下树:
library (data.tree)
data (acme)
t1<-acme
> acme
levelName
1 Acme Inc.
2 ¦--Accounting
3 ¦ ¦--New Software
4 ¦ °--New Accounting Standards
5 ¦--Research
6 ¦ ¦--New Product Line
7 ¦ °--New Labs
8 °--IT
9 ¦--Outsource
10 ¦--Go agile
11 °--Switch to R
我想通过在每个节点名称中添加行数来枚举树节点名称,如下所示:
> t1
levelName
1 Acme Inc._1
2 ¦--Accounting_2
3 ¦ ¦--New Software_3
4 ¦ °--New Accounting Standards_4
5 ¦--Research_5
6 ¦ ¦--New Product Line_6
7 ¦ °--New Labs_7
8 °--IT_8
9 ¦--Outsource_9
10 ¦--Go agile_10
11 °--Switch to R_11
答案 0 :(得分:5)
我们可以使用Get
遍历树,从中收集name
并连接(paste0
)从1到totalCount
。然后使用Set
遍历树并分配值:
acme$Set(name = paste0(acme$Get("name"), "_", 1:acme$totalCount))
print(acme)
给出了:
# levelName
#1 Acme Inc._1
#2 ¦--Accounting_2
#3 ¦ ¦--New Software_3
#4 ¦ °--New Accounting Standards_4
#5 ¦--Research_5
#6 ¦ ¦--New Product Line_6
#7 ¦ °--New Labs_7
#8 °--IT_8
#9 ¦--Outsource_9
#10 ¦--Go agile_10
#11 °--Switch to R_11