如何在R中使用节点属性创建网络图的子集

时间:2020-07-05 04:06:18

标签: networking subgraph statnet

我希望绘制一个网络图。如果节点数超过20万,则该图“超出范围”,无法绘制。

为解决此问题,我打算通过根据日期(节点属性)过滤节点来使用节点属性创建节点的子集(子图),即我将过滤2000年及之前的节点。

但是,“ dates”变量(节点属性)为字符形式。更改为日期(而不是字符)后,network()函数将不起作用。

这意味着我无法使用日期创建子图,除非手动逐个指定日期,否则无法使用100k以上的日期。有什么解决方法吗?

library(statnet)
library(tidyverse)

# Reading in the edgelist and node attributes
edgelist = read.delim("Data/nodes.txt", header = TRUE, sep = "\t") #edgelist
edgelist$From = as.character(edgelist$From)
edgelist$To = as.character(edgelist$To)

node_attr = read.delim("Data/dates.txt", header = TRUE, sep = "\t") #attributes
node_attr$Date = as.character(node_attr$Date)
node_attr$ID = as.character(node_attr$ID)

# Creating a network
network_el = network(edgelist, directed=T, hyper=F, loops=F, multiple=F, bipartite=F)

# Attaching attributes to the network
network_at = network(edgelist, vertex.attr=node_attr, vertex.attrnames=colnames(node_attr),
                  directed=T, hyper=F, loops=F, multiple=F, bipartite=F) #dates need to be in char for this code to run

# Create a subset (graph is too huge - create a subset by filtering Dates)
# I would like to get a network of nodes with Dates 2000-01-01 or before
# But the Dates are in 'character' form. 

n1001 = get.inducedSubgraph(network_at, which(network_at %v% "Date" == "1992-02-24")) 
gplot(n1001)

network_at的dput()会产生以下结果:

structure(list(mel = list(list(inl = 11817L, outl = 10L, atl = list(
    na = FALSE)), list(inl = 12461L, outl = 10L, atl = list(na = FALSE)), 
    list(inl = 12575L, outl = 10L, atl = list(na = FALSE)), list(
        inl = 12839L, outl = 10L, atl = list(na = FALSE)), list(
        inl = 13241L, outl = 10L, atl = list(na = FALSE)), list(
        inl = 13696L, outl = 10L, atl = list(na = FALSE)))

0 个答案:

没有答案