R语言 根据应用于两条边的条件创建二分投影

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

我有一个数据集,它有不同的源节点和目标节点,还有一个与关系相关的数值变量。
它看起来有点像这样:

  1. library(igraph)
  2. library(tidygraph)
  3. set.seed(24601)
  4. example_data <-
  5. data.frame(source =
  6. sample(letters[1:10],
  7. 100,
  8. replace = TRUE),
  9. target =
  10. sample(letters[16:25],
  11. 100,
  12. replace = TRUE),
  13. important_variable =
  14. rnorm(100))

字符串
假设source的成员是个人,target的成员是他们去过的不同城市,我想创建一个网络,显示同一个人何时访问了两个给定的城市。我会使用bipartite_projection(),如下所示:

  1. example_data %>%
  2. graph_from_data_frame() %>%
  3. as_tbl_graph() %>%
  4. mutate(type =
  5. ifelse(name %in% letters[1:10],
  6. TRUE,
  7. FALSE)) %>%
  8. bipartite_projection(which = "true")


然而,我只想在满足一定条件的情况下连接不同的城市:例如,当important_variable的值之间的差异最大为0.5时(例如,当同一个人在同一年访问过两个城市时,我感兴趣)。目前,使用bipartite_projection后,来自important_variable的信息被丢弃。
我看不出有什么方法可以限制基于第三个数字变量的bipartite_projection。有可能吗?提前感谢您的帮助。

使用edit更新以显示所需的输出:

让我们看一小部分的行:

  1. example_data %>%
  2. filter(source == "a") %>%
  3. head()

这将产生以下结果:

  1. source target important_variable
  2. 1 a x 0.29773720
  3. 2 a p 1.50474490
  4. 3 a y 0.01149263
  5. 4 a q 0.19391773
  6. 5 a t -0.10656946
  7. 6 a w -0.29516668

我可以直接进入二分投影,像这样:

  1. example_data %>%
  2. filter(source == "a") %>%
  3. head() %>%
  4. graph_from_data_frame() %>%
  5. as_tbl_graph() %>%
  6. mutate(type =
  7. ifelse(name %in% letters[1:10],
  8. TRUE,
  9. FALSE)) %>%
  10. bipartite_projection(which = "false")

其产生具有一个顶点属性name和一个边属性node的iGraph对象。
然而,我想要看起来像这样的东西(为了简单起见,只有前四行):

  1. source_projected target_projected source_att target_att
  2. 1 x p 0.2977372 1.50474490
  3. 2 x y 0.2977372 0.01149263
  4. 3 x q 0.2977372 0.19391773
  5. 4 x t 0.2977372 -0.10656946

因为这将允许我根据source_atttarget_att列之间的关系进行过滤(例如,过滤source_atttarget_att之间的差异小于0.5)

第二次更新,更详细的期望输出

@ JumasIsCoding提供了一个符合我要求的解决方案。这让我意识到我没有足够详细。
再次从原始数据开始,我们可以看到a链接到p两次,a链接到y两次。在每种情况下,important_variable的值都是不同的。如下所示:

  1. example_data %>%
  2. filter(source == "a" &
  3. (target == "p" |
  4. target == "y"))
  5. source target important_variable
  6. 1 a p 1.50474490
  7. 2 a y 0.01149263
  8. 3 a y -2.34069094
  9. 4 a p 0.29294049

我发布的示例所需数据仅包括target中的每个节点连接一次。然而,由于important_variable的值不同,我希望输出包括这些配对的所有配置,如下所示:

  1. source_projected target_projected source_att target_att
  2. 1 p y 0.2977372 0.01149263
  3. 2 p y 0.2977372 -2.34069094
  4. 3 p y 0.2929405 0.01149263
  5. 4 p y 0.2929405 -2.34069094

这是一个可以构建的东西吗?谢谢!

3pmvbmvn

3pmvbmvn1#

更新

由于单个目标可能有多个值,我想最好使用left_join并为relationship参数启用"many-to-many"

  1. out <- example_data %>%
  2. graph_from_data_frame() %>%
  3. set_vertex_attr(
  4. name = "type",
  5. value = names(V(.)) %in% example_data$target
  6. ) %>%
  7. bipartite_projection() %>%
  8. pluck("proj2") %>%
  9. as_data_frame() %>%
  10. select(-weight) %>%
  11. left_join(select(example_data, -source),
  12. join_by(from == target),
  13. relationship = "many-to-many"
  14. ) %>%
  15. left_join(select(example_data, -source),
  16. join_by(to == target),
  17. relationship = "many-to-many"
  18. ) %>%
  19. rename(all_of(c(source_att = "important_variable.x", target_att = "important_variable.y")))

字符串
你会看到

  1. > head(out)
  2. from to source_att target_att
  3. 1 x y 0.2977372 0.50506407
  4. 2 x y 0.2977372 -1.37333412
  5. 3 x y 0.2977372 0.61981223
  6. 4 x y 0.2977372 0.43724194
  7. 5 x y 0.2977372 -1.97363488
  8. 6 x y 0.2977372 -0.02413137
  9. > glimpse(out)
  10. Rows: 4,462
  11. Columns: 4
  12. $ from <chr> "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x",…
  13. $ to <chr> "y", "y", "y", "y", "y", "y", "y", "y", "y", "y", "y", "y",…
  14. $ source_att <dbl> 0.2977372, 0.2977372, 0.2977372, 0.2977372, 0.2977372, 0.29
  15. $ target_att <dbl> 0.50506407, -1.37333412, 0.61981223, 0.43724194, -1.9736348

上一页

也许你可以试试下面的代码

  1. example_data %>%
  2. graph_from_data_frame() %>%
  3. set_vertex_attr(
  4. name = "type",
  5. value = names(V(.)) %in% example_data$target
  6. ) %>%
  7. bipartite_projection() %>%
  8. pluck("proj2") %>%
  9. as_data_frame() %>%
  10. select(-weight) %>%
  11. mutate(
  12. source_att = with(example_data, important_variable[match(from, target)]),
  13. target_att = with(example_data, important_variable[match(to, target)])
  14. )

这给

  1. from to source_att target_att
  2. 1 x y 0.29773720 0.50506407
  3. 2 x p 0.29773720 -0.74022203
  4. 3 x u 0.29773720 -2.04969760
  5. 4 x q 0.29773720 1.36281039
  6. 5 x w 0.29773720 -0.47578690
  7. 6 x s 0.29773720 0.03233063
  8. 7 x t 0.29773720 -1.08378137
  9. 8 x r 0.29773720 -0.72029435
  10. 9 x v 0.29773720 -0.22919308
  11. 10 y p 0.50506407 -0.74022203
  12. 11 y u 0.50506407 -2.04969760
  13. 12 y q 0.50506407 1.36281039
  14. 13 y w 0.50506407 -0.47578690
  15. 14 y s 0.50506407 0.03233063
  16. 15 y t 0.50506407 -1.08378137
  17. 16 y r 0.50506407 -0.72029435
  18. 17 y v 0.50506407 -0.22919308
  19. 18 p u -0.74022203 -2.04969760
  20. 19 p q -0.74022203 1.36281039
  21. 20 p w -0.74022203 -0.47578690
  22. 21 p s -0.74022203 0.03233063
  23. 22 p t -0.74022203 -1.08378137
  24. 23 p r -0.74022203 -0.72029435
  25. 24 p v -0.74022203 -0.22919308
  26. 25 r u -0.72029435 -2.04969760
  27. 26 r q -0.72029435 1.36281039
  28. 27 r w -0.72029435 -0.47578690
  29. 28 r s -0.72029435 0.03233063
  30. 29 r t -0.72029435 -1.08378137
  31. 30 r v -0.72029435 -0.22919308
  32. 31 u q -2.04969760 1.36281039
  33. 32 u w -2.04969760 -0.47578690
  34. 33 u s -2.04969760 0.03233063
  35. 34 u t -2.04969760 -1.08378137
  36. 35 u v -2.04969760 -0.22919308
  37. 36 v s -0.22919308 0.03233063
  38. 37 v t -0.22919308 -1.08378137
  39. 38 v q -0.22919308 1.36281039
  40. 39 v w -0.22919308 -0.47578690
  41. 40 q w 1.36281039 -0.47578690
  42. 41 q s 1.36281039 0.03233063
  43. 42 q t 1.36281039 -1.08378137
  44. 43 w s -0.47578690 0.03233063
  45. 44 w t -0.47578690 -1.08378137
  46. 45 s t 0.03233063 -1.08378137

然后我猜你知道如何过滤行,并限制source_atttarget_att之间的差异。

展开查看全部

相关问题