Re: [问题] data.frame依字段顺序转成树状lists

楼主: andrew43 (讨厌有好心推文后删文者)   2017-11-17 15:29:58
不用刻轮子喔。这里简单地把你的例子用 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 |
作者: cywhale (cywhale)   2017-11-17 22:28:00
原来keepOnly是这样用法!太感谢了!
楼主: andrew43 (讨厌有好心推文后删文者)   2017-11-17 19:14:00
改了一下看看是不是符合需求?现在还有留下 ...$terminalName,但干净很多了,你再改
作者: cywhale (cywhale)   2017-11-17 16:44:00
非常感谢,tree结构稍微修改pathString 可以拿掉NA ok~但list它多了一层把names of node都留下来我想看看怎么改..

Links booklink

Contact Us: admin [ a t ] ucptt.com