r - 如何在 R 中创建单分支实体/死胡同的外部叶列表?

标签 r list

我有一个相当大的树状结构/树状图/网络(认为谱系),我想创建一个单数连接的叶子/节点的列表。

在谱系学中,我认为类似的东西被称为“Spitzenahnen”(德语)/但我认为它特定于“没有已知的 parent ”,不一定没有后代。所以基本上是结构中的死胡同,而不仅仅是顶部或底部才是我想要找到的。

我看到了post on creating a edge list从矩阵以及如何 access the attributes of a dendrogram在 R 中,但不确定如何应用它来获得我想要获得的具体结果。

我有数千个具有多个起点和终点的节点。我想创建一个节点/叶子列表,其中只有一个连接节点将其附加到树上。因此,如果该节点有两个或多个连接(有些最多有两打),我不想在我的列表中看到它。

使用赵京华的“Drawing pedigree diagrams with R and graphviz”中的标记图形,我只想看到突出显示的节点,但一些适用的节点可能深埋在网络深处,而不一定在“边缘”。

enter image description here

最佳答案

您似乎正在使用此数据:

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"

哪个产生

enter image description here

(或类似的东西,默认布局算法是随机的)。

reshape 数据有点困惑,但想法是我为每个并集创建伪节点,从而产生一个子节点。然后我将父节点连接为传入节点,将子节点连接为传出节点。

基本上你描述的节点都有一个连接,这意味着在图形设置中,它们的度数都是1。我们可以将这些标签更改为红色以获得

V(gg)$label.color<-"black"
V(gg)[degree(gg)==1 & type=="person"]$label.color<-"red"

plot(gg)

enter image description here

或者你可以直接获取名称

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/

相关文章:

c# - 如何合并两个列表并删除重复项

r - 如何计算矩阵中两个元素之间的最大欧氏距离 - R?

r - 填充 R data.frame 中每行中缺失的元素

r - 忽略 R 函数中的错误 - try() 函数不起作用

python - 列表更改意外反射(reflect)在子列表中

python - 如何从字典键列表中提取特定项目

c - 删除 list.h 列表的第一个元素

javascript - 如何格式化使用 R DT(数据表)包生成的表的标题

在 R 中使用多个条件按组返回最大值

python:找到两个数组中两点之间的最小距离