R_Language 板


LINE

不用刻輪子喔。這裡簡單地把你的例子用 package data.tree 解析。 library(data.tree) library(data.table) library(magrittr) dt <- fread('A B C D a1 b1 NA NA a2 b2 cx1 NA a2 b2 cx1 dxx a2 b2 cx2 NA a3 b3 cx3 dx1 a3 b3 cx3 dx2 a4 b4 cx4 dx3') # append a column to describe terminals dt[, pathString := paste("theRoot", A, B, C, D, sep = "/")] # convert data frame to a tree dt.tree <- as.Node(dt) dt.tree # levelName # 1 theRoot # 2 |--a1 # 3 | °--b1 # 4 | °--NA # 5 | °--NA # 6 |--a2 # 7 | °--b2 # 8 | |--cx1 # 9 | | |--NA # 10 | | °--dxx # 11 | °--cx2 # 12 | °--NA # 13 |--a3 # 14 | °--b3 # 15 | °--cx3 # 16 | |--dx1 # 17 | °--dx2 # 18 °--a4 # 19 °--b4 # 20 °--cx4 # 21 °--dx3 plot(dt.tree) # see https://imgur.com/a/zja70 # convert tree to a list dt.tree.list <- as.list(dt.tree$root) dt.tree.list$a1 dt.tree.list$a1$b1 ※ 引述《cywhale》之銘言: : [問題敘述]: : 想得到依欄位展開的樹狀結構list,且在leaf nodes 保留 names of leaf nodes : 嘗試了幾次沒有得到很好的結果,以下是最接近的方式,想請教~ : library(data.table) : library(magrittr) : dt <- fread('A B C D : a1 b1 NA NA : a2 b2 cx1 NA : a2 b2 cx1 dxx : a2 b2 cx2 NA : a3 b3 cx3 dx1 : a3 b3 cx3 dx2 : a4 b4 cx4 dx3') : out <- dt %>% split(.$A) %>% : lapply(function(x) : lapply(split(x, x$B), function(y) : lapply(split(y, y$C), function(z) { : sapply(z$D, function(u) { : if (is.na(u)) return(names(u)) : list(u)}) : }))) : out : #================以下輸出結果 : $a1 : $a1$b1 : named list() #這是錯的結果,想得到"b1" : $a2 : $a2$b2 : $a2$b2$cx1 : $a2$b2$cx1$<NA> #這是錯的結果 到$cx1 即停止 : NULL ##這是錯的結果 ,想得到"cx1" : $a2$b2$cx1$dxx : $a2$b2$cx1$dxx[[1]] : [1] "dxx" ##這是對的結果,留下name of leaf node : $a2$b2$cx2 : $a2$b2$cx2$<NA> #這是錯的結果 到$cx2 即停止 : NULL #這是錯的結果 ,想得到"cx2" : $a3 : $a3$b3 : $a3$b3$cx3 : $a3$b3$cx3$dx1 : [1] "dx1" ##這是對的結果 : $a3$b3$cx3$dx2 : [1] "dx2" ##這是對的結果 : $a4 : $a4$b4 : $a4$b4$cx4 : $a4$b4$cx4$dx3 : [1] "dx3" ##這是對的結果 : 如上結果,我想得到依欄位ABCD 展開的樹狀結構list,所以用了split : 可以正確地依階層順序展開 ($A -> $B -> $C -> $D) : 而且想在leaf nodes 保留 names of leaf nodes : 比如 $a3$b3$cx3$dx1 [1] "dx1" 這是對的結果 : 但 像後面有NA 的node 在 split 時就會被拋棄,因此我得不到node名 : 我希望得到 : $a1$b1 : [1] "b1" : $a2$b2$cx1 : [1] "cx1" : $a2$b2$cx2 : [1] "cx2" : 不知道有無解法?謝謝~~ : (附註,上面a1, b1, c1 字母和數字均為範例無意義,真實資料中為任意字串,請勿依數 : 字規則來當作程式一部分,僅是舉例) : ※ 編輯: cywhale (140.112.65.48), 11/17/2017 11:28:28 --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.222.1
※ 文章網址: https://webptt.com/m.aspx?n=bbs/R_Language/M.1510903800.A.010.html ※ 編輯: andrew43 (60.248.222.1), 11/17/2017 15:30:58
1F:推 cywhale: 非常感謝,tree結構稍微修改pathString 可以拿掉NA ok~但 11/17 16:44
2F:推 cywhale: list它多了一層把names of node都留下來我想看看怎麼改.. 11/17 16:46
如果是這樣呢? library(data.tree) library(data.table) library(magrittr) dt <- fread('A B C D a1 b1 NA NA a2 b2 cx1 NA a2 b2 cx1 dxx a2 b2 cx2 NA a3 b3 cx3 dx1 a3 b3 cx3 dx2 a4 b4 cx4 dx3') # append a column to describe structure dt[, pathString := paste("theRoot", A, B, C, D, sep = "/") %>% gsub("/NA", "", .)] # append a column to record terminal name dt[, terminalName := strsplit(pathString, "/") %>% sapply(., last) ] # convert data frame to a tree and then convert to a list dt.tree <- as.Node(dt) as.list(dt.tree, keepOnly = "terminalName") ※ 編輯: andrew43 (60.248.222.1), 11/17/2017 19:13:56
3F:→ andrew43: 改了一下看看是不是符合需求? 11/17 19:14
※ 編輯: andrew43 (60.248.222.1), 11/17/2017 19:27:56
4F:→ andrew43: 現在還有留下 ...$terminalName,但乾淨很多了,你再改 11/17 19:32
5F:推 cywhale: 原來keepOnly是這樣用法!太感謝了! 11/17 22:28







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Tech_Job站內搜尋

TOP