通过ID的data.tree节点 [英] data.tree nodes through Id's

查看:88
本文介绍了通过ID的data.tree节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据通过IdParentId系统链接,并且我设法添加了正确的整数levels,但是,我想编写一个函数,该函数自动将我的5层层次结构嵌套为pathString用于data.tree.

My data is linked through an Id, ParentId system and I have managed to add correct integer levels, however, I would like to compose a function that automatically nests my 5 tiered hierarchy as a pathString for data.tree.

结构:

Id                 Name               ParentId           ParentName    Level
701F0000006Iw8E    'Paid Media'       NA                 NA            1
701F0000006IS1t    'Bing ABC'         701F0000006Iw8Y    'Bing'        3    
701F0000006IS28    'Bing DEF'         701F0000006Iw8Y    'Bing'        3
701F0000006IS23    'Bing GHI'         701F0000006Iw8Y    'Bing'        3
701F0000006Imq9    'Bing JKL'         701F0000006Iw8Y    'Bing'        3
701F0000006IS1y    'Bing MNO'         701F0000006Iw8Y    'Bing'        3
701F0000006Iw8Y    'Bing'             701F0000006Iw8E    'Paid Media'  2
701F0000006IvcW    'Google'           701F0000006Iw8E    'Paid Media'  2
7012A000006rhY8    'Adwords ABC'      701F0000006IvcW    'Google'      3
701F0000006IS1j    'Adwords DEF'      701F0000006IvcW    'Google'      3
701F0000006IS1o    'Adwords GHI'      701F0000006IvcW    'Google'      3
701F0000006IS1Z    'Adwords JKL'      701F0000006IvcW    'Google'      3
701F0000006Ieci    'Adwords MNO'      701F0000006IvcW    'Google'      3

当前,我遇到以下问题:pathString在以下情况下只能由单个层读取:

Currently, I run into the issue that pathString gets read only by a single tier in the following:

dat$pathString <- paste(dat$ParentId, 
      dat$Id, 
      sep = "/")

例如

 "NA/701F0000000SOEq"

实际上,要正确地填充整个树,我需要在字符串中标识所有后续的父级:

Which, in reality to populate the whole tree correctly, I would need to identify all subsequent parents within the string:

 "NA/701F0000006Iw8E/701F0000006Iw8Y/701F0000006IS1t" for "Bing ABC"

理想情况下,单个表达式在所有级别上都等效,但是我知道每个级别是否需要单独处理.

Ideally, a single expression will work equivalently for all levels but I understand if each level needs to be handled separately.

此处是完整ID,ParentId系统: Dropbox链接

Full Id,ParentId system here: Dropbox Link

推荐答案

尽管您的问题要求输入路径字符串,但也可以直接从您的数据帧格式构建树.

Although your question asks for a path string, the tree can be built directly from your data frame format.

library(data.tree)
dat <- read.table(text="
Id                 Name               ParentId           ParentName    Level
701F0000006Iw8E    'Paid Media'       NA                 NA            1
701F0000006IS1t    'Bing ABC'         701F0000006Iw8Y    'Bing'        2    
701F0000006IS28    'Bing DEF'         701F0000006Iw8Y    'Bing'        2
701F0000006IS23    'Bing GHI'         701F0000006Iw8Y    'Bing'        2
701F0000006Imq9    'Bing JKL'         701F0000006Iw8Y    'Bing'        2
701F0000006IS1y    'Bing MNO'         701F0000006Iw8Y    'Bing'        2
701F0000006Iw8Y    'Bing'             701F0000006Iw8E    'Paid Media'  3
701F0000006IvcW    'Google'           701F0000006Iw8E    'Paid Media'  3
7012A000006rhY8    'Adwords ABC'      701F0000006IvcW    'Google'      2
701F0000006IS1j    'Adwords DEF'      701F0000006IvcW    'Google'      2
701F0000006IS1o    'Adwords GHI'      701F0000006IvcW    'Google'      2
701F0000006IS1Z    'Adwords JKL'      701F0000006IvcW    'Google'      2
701F0000006Ieci    'Adwords MNO'      701F0000006IvcW    'Google'      2
", header=TRUE, stringsAsFactors = F)

# network build does not want a root node as a row, so adjust
# the given root to link to "tree_root"
dat$ParentId[is.na(dat$ParentId)] <- "tree_root"

# build the tree using the network layout - pairs of node ids
# in the first two columns. Remaining columns are node attributes
dat_network <- subset(dat, !is.na(dat$ParentId), c("Id", "ParentId", "Name"))
dat_tree <- FromDataFrameNetwork(dat_network, check = "check")

print(dat_tree, 'Name')

# 1  tree_root                              
# 2   °--701F0000006Iw8E          Paid Media
# 3       ¦--701F0000006Iw8Y            Bing
# 4       ¦   ¦--701F0000006IS1t    Bing ABC
# 5       ¦   ¦--701F0000006IS28    Bing DEF
# 6       ¦   ¦--701F0000006IS23    Bing GHI
# 7       ¦   ¦--701F0000006Imq9    Bing JKL
# 8       ¦   °--701F0000006IS1y    Bing MNO
# 9       °--701F0000006IvcW          Google
# 10          ¦--7012A000006rhY8 Adwords ABC
# 11          ¦--701F0000006IS1j Adwords DEF
# 12          ¦--701F0000006IS1o Adwords GHI
# 13          ¦--701F0000006IS1Z Adwords JKL
# 14          °--701F0000006Ieci Adwords MNO

这篇关于通过ID的data.tree节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆