我有一个相当大的树状结构/树状图/网络(认为谱系),我想创建一个单数连接的叶子/节点的列表。
在谱系学中,我认为类似的东西被称为“Spitzenahnen”(德语)/但我认为它特定于“没有已知的 parent ”,不一定没有后代。所以基本上是结构中的死胡同,而不仅仅是顶部或底部才是我想要找到的。
我看到了post on creating a edge list从矩阵以及如何 access the attributes of a dendrogram在 R 中,但不确定如何应用它来获得我想要获得的具体结果。
我有数千个具有多个起点和终点的节点。我想创建一个节点/叶子列表,其中只有一个连接节点将其附加到树上。因此,如果该节点有两个或多个连接(有些最多有两打),我不想在我的列表中看到它。
使用赵京华的“Drawing pedigree diagrams with R and graphviz”中的标记图形,我只想看到突出显示的节点,但一些适用的节点可能深埋在网络深处,而不一定在“边缘”。
最佳答案
您似乎正在使用此数据:
pre <- read.table(text="pid id father mother sex affected
10081 1 2 3 2 1
10081 2 0 0 1 2
10081 3 0 0 2 1
10081 4 2 3 2 1
10081 5 2 3 2 2
10081 6 2 3 1 2
10081 7 2 3 2 2
10081 8 0 0 1 2
10081 9 8 4 1 2
10081 10 0 0 2 2
10081 11 2 10 2 2
10081 12 2 10 2 1
10081 13 0 0 1 2
10081 14 13 11 1 2
10081 15 0 0 1 2
10081 16 15 12 2 2",header=T)
如果您正在查看类似图形的数据,您可以考虑使用 igraph
库。这是创建类似情节的一种方法。
unit<-as.character(interaction(pre$father, pre$mother))
el<-rbind(
data.frame(person=as.character(c(pre$father, pre$mother)), unit=unit, stringsAsFactors=F),
data.frame(person=unit, unit=pre$id, stringsAsFactors=T)
)
el<-subset(el, person!="0" & person !="0.0" & unit!="0" & unit!="0.0")
gg<-simplify(graph.data.frame(el, vertices=rbind(
data.frame(id=pre$id, type="person", affected=pre$affected==1, sex=pre$sex),
data.frame(id=unique(unit), type="family", affected=FALSE, sex=0))))
V(gg)$color <- "grey"
V(gg)[type=="person" & !affected]$color <- "deepskyblue"
V(gg)$label <- ""
V(gg)[type=="person"]$label <- V(gg)$name
V(gg)$size <-2
V(gg)[type=="person"]$size <- 15
V(gg)$shape<-"circle"
V(gg)[sex==1]$sex<-"square"
哪个产生
(或类似的东西,默认布局算法是随机的)。
reshape 数据有点困惑,但想法是我为每个并集创建伪节点,从而产生一个子节点。然后我将父节点连接为传入节点,将子节点连接为传出节点。
基本上你描述的节点都有一个连接,这意味着在图形设置中,它们的度数都是1。我们可以将这些标签更改为红色以获得
V(gg)$label.color<-"black"
V(gg)[degree(gg)==1 & type=="person"]$label.color<-"red"
plot(gg)
或者你可以直接获取名称
V(gg)[degree(gg)==1 & type=="person"]$name
# [1] "1" "3" "5" "6" "7" "8" "9" "10" "13" "14" "15" "16"
关于r - 如何在 R 中创建单分支实体/死胡同的外部叶列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29284968/