0

So I would like to evaluate homophily against a random network. This means I need to create a random network with the attributes that I am interested in. Getting the random network is easy enough, but I can't add attributes because the random networks don't have anything I can join by...

gr = play_erdos_renyi(6, 0.3)

Attr = sample.int(3, 6, replace = TRUE

gr <- gr %>%
 activate(nodes) %>%
 join(Attr)

I think I could first generate the random network, then spit that into an edgelist, then add attributes to the edgelist, and then use graph_from_data_frame to get the network, but it feels like there should be some easier way to do this.

EBrewe
  • 113
  • 10

1 Answers1

0

You can simply mutate and assign the attributes to your new graph.

library(tidygraph)

set.seed(2021)

gr <- play_erdos_renyi(6, 0.3)

Attr <- sample.int(3, 6, replace = TRUE)

gr <- gr %>%
 activate(nodes) %>%
 mutate(attr = Attr)

gr
# # A tbl_graph: 6 nodes and 9 edges
# #
# # A directed simple graph with 1 component
# #
# # Node Data: 6 x 1 (active)
#    attr
#   <int>
# 1     2
# 2     2
# 3     1
# 4     2
# 5     3
# 6     2
# #
# # Edge Data: 9 x 2
#    from    to
#   <int> <int>
# 1     3     1
# 2     5     1
# 3     6     1
# # … with 6 more rows
Eric Leung
  • 2,354
  • 12
  • 25