iGraph收缩网络中顶点属性的处理

brgchamk  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(136)

这个查询跟随另一个关于折叠网络保留属性信息的查询(contract vertices in igraph r preserving all nodes attributes?)。我一直在尝试从igraph属性对象中提取文本。属性不是原子向量,因为它们是igraph的合同函数的结果,作为一种获取字符和数字属性数据的方法:

g <- graph_from_literal(1,2-3-4,5-6,7).
V(g)$label <- c("wood", "wood products", "work", "work environment",
                "work environments", "young", "young consumers")
V(g)$number <- c(1,2,1,2,1,2,2)
g <- contract(g, mapping=c(1,1,3,4,4,6,7), vertex.attr.comb=c)
g <- delete.vertices(g, which(V(g)$name=="character(0)"))

字符串
我只需要一个标签,例如第一个,在有多个的情况下。我尝试:

first <- stri_extract_first_regex(V(g)$label, "[a-z]+")


但是在单个文本的情况下,结果会产生“c”,而不是相应的标签。
先谢了。

bf1o4zei

bf1o4zei1#

如果需要保留所有属性的第一个条目,则应使用vertex.attr.comb = first

g <- contract(g, mapping = c(1, 1, 3, 4, 4, 6, 7), vertex.attr.comb = first)

字符串
你会发现

> V(g)$label
[[1]]
[1] "wood"

[[2]]
character(0)

[[3]]
[1] "work"

[[4]]
[1] "work environment"

[[5]]
character(0)

[[6]]
[1] "young"

[[7]]
[1] "young consumers"

> V(g)$name
[[1]]
[1] "1"

[[2]]
character(0)

[[3]]
[1] "3"

[[4]]
[1] "4"

[[5]]
character(0)

[[6]]
[1] "6"

[[7]]
[1] "7"


此外,如果您想移除具有“空”属性的顶点,可以尝试

g <- contract(g, mapping = c(1, 1, 3, 4, 4, 6, 7), vertex.attr.comb = first)
g <- delete.vertices(g, lengths(V(g)$name) == 0)


然后才能看

> V(g)$label
[[1]]
[1] "wood"

[[2]]
[1] "work"

[[3]]
[1] "work environment"

[[4]]
[1] "young"

[[5]]
[1] "young consumers"

> V(g)$name
[[1]]
[1] "1"

[[2]]
[1] "3"

[[3]]
[1] "4"

[[4]]
[1] "6"

[[5]]
[1] "7"

相关问题