2015-11-01 66 views
-1

我有以下三種:添加數字到每個節點data.tree

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 
+1

請給出[重複的例子(http://stackoverflow.com/questions/5963269/how-to -ma-r-reproducible-example/5963610) – Jaap

+0

Hi @Jaap。如果你使用庫(data.tree),你將能夠使用示例數據(acme)。它在包中。 – Avi

回答

4

我們可以使用Get遍歷樹,收集name,並從1沿途串聯(paste0)到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