R--data.tree(1)

Introduction to data.tree


the data.tree package lets you create hierarchies,called data.tree structures.
traversal search sort operations, recursive tree programming
decorate Nodes with own fields and methods, extend the package to your nodes

neatly printing and plotting trees
conversion from and to data.frames lists and other tree structures such as dendrogram phylo objects from ape packages igraph and other

data.tree structures are bi-directional,ordered trees.

Node

#attribute: active,field,method.  Get
#active: (property) a field on a node like node$position(node本身有的字段)
#field: a named value on a Node like node$cost <- 2500(用戶自定義的字段)
#method: a function acting on an node like node$Revert() (OO) or Revert(node) (traditional)
#inheritance: a child Node inherits attribute from one of its ancestors. Get,SetNodeStyle```

#Tree creation

建造樹的時候需要注意的是,只能使用$AddChild建造下一層的樹葉子,不能跨層建立

建造的時候可以指定此次建造的葉子的變量,可以使用這個變量用$AddChild建造下一層的葉子

library(data.tree)
acme <- Node$new("Acme")
accounting <- acme$AddChild("Accounting")
software <- accounting$AddChild("software")
print(acme)

each Node is identified by its name

the name needs to be unique among siblings,such that paths to Nodes are unambiguous(清楚的)

如果不指定每個Node的name,那么將會使用AddChild()里面的默認字符串作為name

acme$AddChild("data")
acme$Get("name")

另外一種獲取name的方法

acme$children[[1]]$name

輸出字符串 Acme Accounting software data

向量 "Acme" "Accounting" "software" "data"

那我就可以寫一個算法,將所有name存在一樣的先改過來,不改變字符串的值,只是改變name

已經搞定

從data.frame建造樹

要點是要確定數據框數據的pathString

library(data.tree)
library(treemap)
data(GNI2014)
head(GNI2014)

這一步是關鍵

GNI2014$pathString <- paste("world",GNI2014$continent,GNI2014$country,sep="/")
population <- as.Node(GNI2014)

print的打印的時候可以選擇的參數

print(population,"iso3","population","GNI",limit=30)

Node exhibits reference semantics.R中所有的變量都是引用,直接修改當地的值

OO-style actives: Node$isRoot

OO-style methods: Node$Prune(pruneFun)

Classical R methods: Clone(node)

對,可以先復制過來樹,然后建造刪除函數,把每層位置大于10的節點都刪除掉

然后給每一層添加一個省略號節點,然后再繪圖

paste(rep(".",3),sep = "")

只顯示15個項目的內容

print(population, limit = 15)```

Actives

#population$isRoot
#population$height
#children of node 可以用這個進行判斷,如果children大于10就開始進行調用那個刪除函數
population$count
#total node in the tree
population$totalCount
#population$fields
#population$fieldsAll
#population$averageBranchFactor```

#OO-style Methods

population$Get("population",filterFun = isLeaf)
population$Prune(pruneFun = function(x) !x$isLeaf || x$population>10000)

那我要定義的刪除函數就是刪除每個節點下位置大于10的節點

population$Get("population",filterFun = isLeaf)```

Traditional R methods

popClone <- Clone(acme)```

#tree navigation

by path 一層一層的索引

acme$IT$outsource

by position通過位置

acme$children[[1]]$children[[2]]$name

by fields

navigate by name, also by other fields with Climb method.

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

acme$Climb(position=1,name="software")$path

and as a shortcut you can climb mutiple levels with a single argument

tree <- CreateRegularTree(5,5)
tree$Climb(position=c(2,3,4))$name

下面是一個比較增深理解的例子,先找到根節點的2位置的child,然后找到child的3位置的child

然后找到名字是1.2.3.4的child,繼續最后尋找到名字是1.2.3.4.5的child

tree$Climb(position=c(2,3),name=c("1.2.3.4","1.2.3.4.5"))$path```

Custom fields

#we can add any custom field to any Node in a data.frame structure
software$cost <- 1000000
software$p <- 0.5
print(acme, "cost", "p")
#and there is a list of reserved names you cannot use as Node fields
#custom fields in constructor
#assign custom fields in the constructor or in the Node$AddChild method
birds <- Node$new("Aves", vulgo = "Bird")
birds$AddChild("Neognathae", vulgo = "New Jaws", species = 10000)
birds$AddChild("Palaeognathae", vulgo = "Old Jaws", species = 60)
print(birds, "vulgo", "species")
#custom fields as function
#setting a function as a field
#計算子節點這個字段的總和
birds$species <- function(self) sum(sapply(self$children, function(x) x$species))
print(birds, "species")
#and data.tree maps the self argument to the Node at hand
#this and Set method and recursion(遞歸), becomes a very powerful tool```

#Printing

on the left, you have the hierachy,then you have a column per variable you want to print

print(acme,"cost","p")

Formatters

you can use formatters to output a variable in a certain way

first you can set them on a Node using the SetFormat

and the formatter will be picked up as a default formatter

So you can overwrite a formatter for a sub-tree

SetFormat(acme,"p",formatFun = FormatPercent)
SetFormat(acme,"cost",formatFun = function(x) FormatFixedDecimal(x,digits=2))
print(acme,"p","cost")

Printing using Get

Formatting with the Get method overwrites any formatters found along the path

data.frame(cost=acme$Get("cost",format=function(x) FormatFixedDecimal(x,2)),
p=acme$Get("p",format=FormatPercent))```

Plotting

plot(acme)
#現在plot的當務之急是要把這個葉子分開,要不擠到那里還是非常難看的
#igraph
library(igraph)
plot(as.igraph(acme,directed=T,direction=T))```
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,622評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,716評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,746評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,991評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,706評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,036評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,029評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,203評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,725評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,451評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,677評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,161評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,857評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,266評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,606評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,407評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,643評論 2 380

推薦閱讀更多精彩內容

  • “下班沒?我回西安了。剛下車,想吃你家對面的那家煎餅。” 肖給我打電話的時候,我剛剛結束一天的忙碌,準備下班。這個...
    晴空曉曉閱讀 459評論 6 3
  • 剛剛過去的中考讓本是緊張不安的我是有點的平靜,不再有在那之前的波動,心中卻是多了一點激動,每天在床上翻來覆去的睡不...
    浩恩閱讀 190評論 0 1
  • 房間里還殘留著些許氣息 僅剩的三炷香也快燃盡 待這香的味道消失殆盡 那股執念也會隨著時間和空間的變化慢慢放下吧! ...
    流光溢彩紛呈閱讀 207評論 0 0
  • 首先說一下Actor Model,作為一種進程或者線程間的通信模型,一般來說有兩種選擇,一種是CSP,比如Go語言...
    墨弈閱讀 3,405評論 0 51
  • ??去掉多余的cell分割線,自定義一個tableFooterView就好: cell分割線樣式 tableVie...
    windyfat閱讀 575評論 0 0