如何打印tbl_graph对象的更多行

huangapple go评论105阅读模式
英文:

How to print more lines of a tbl_graph object

问题

  1. 如何打印更多行?
  2. 我已经阅读了关于整洁数据框的各种解决方案,但没有找到关于 "tbl_graph" "igraph" 对象的解决方案。
  3. 谢谢
英文:

I have the following tidy graph

  1. library(tidygraph)
  2. library(tibble)
  3. mynodes <- tibble(id = c(1,2,3,4,5,6,7,8,9,10))
  4. myedges <- tibble(from = c(1,1,1,5,2,2,4,8,8,8),
  5. to = c(1,2,3,4,3,4,1,2,3,4),
  6. power = c(10,10,10,3,3,3,2,2,2,2))
  7. graph = tbl_graph(nodes = mynodes, edges = myedges)

the output is the following

  1. graph
  2. # A tbl_graph: 10 nodes and 10 edges
  3. #
  4. # A directed multigraph with 5 components
  5. #
  6. # A tibble: 10 × 1
  7. id
  8. <dbl>
  9. 1 1
  10. 2 2
  11. 3 3
  12. 4 4
  13. 5 5
  14. 6 6
  15. # ℹ 4 more rows
  16. # ℹ Use `print(n = ...)` to see more rows
  17. #
  18. # A tibble: 10 × 3
  19. from to power
  20. <int> <int> <dbl>
  21. 1 1 1 10
  22. 2 1 2 10
  23. 3 1 3 10
  24. # ℹ 7 more rows
  25. # ℹ Use `print(n = ...)` to see more rows

How I can print more lines?
I have read various solutions for tidy df but none for "tbl_graph" "igraph" object.
Thank you

答案1

得分: 1

你可以使用 lapply 结合 printnrow 来展示所有行,代码示例如下:

  1. library(tidygraph)
  2. library(tibble)
  3. mynodes <- tibble(id = c(1,2,3,4,5,6,7,8,9,10))
  4. myedges <- tibble(from = c(1,1,1,5,2,2,4,8,8,8),
  5. to = c(1,2,3,4,3,4,1,2,3,4),
  6. power = c(10,10,10,3,3,3,2,2,2,2))
  7. graph = tbl_graph(nodes = mynodes, edges = myedges)
  8. lapply(graph, \(x) x %>% print(n = nrow(.)))

<sup>创建于2023年5月14日,使用 reprex v2.0.2</sup>

英文:

You could use lapply with print and the nrow to show all rows like this:

  1. library(tidygraph)
  2. library(tibble)
  3. mynodes &lt;- tibble(id = c(1,2,3,4,5,6,7,8,9,10))
  4. myedges &lt;- tibble(from = c(1,1,1,5,2,2,4,8,8,8),
  5. to = c(1,2,3,4,3,4,1,2,3,4),
  6. power = c(10,10,10,3,3,3,2,2,2,2))
  7. graph = tbl_graph(nodes = mynodes, edges = myedges)
  8. lapply(graph, \(x) x %&gt;% print(n = nrow(.)))
  9. #&gt; # A tibble: 10 &#215; 1
  10. #&gt; id
  11. #&gt; &lt;dbl&gt;
  12. #&gt; 1 1
  13. #&gt; 2 2
  14. #&gt; 3 3
  15. #&gt; 4 4
  16. #&gt; 5 5
  17. #&gt; 6 6
  18. #&gt; 7 7
  19. #&gt; 8 8
  20. #&gt; 9 9
  21. #&gt; 10 10
  22. #&gt; # A tibble: 10 &#215; 3
  23. #&gt; from to power
  24. #&gt; &lt;int&gt; &lt;int&gt; &lt;dbl&gt;
  25. #&gt; 1 1 1 10
  26. #&gt; 2 1 2 10
  27. #&gt; 3 1 3 10
  28. #&gt; 4 5 4 3
  29. #&gt; 5 2 3 3
  30. #&gt; 6 2 4 3
  31. #&gt; 7 4 1 2
  32. #&gt; 8 8 2 2
  33. #&gt; 9 8 3 2
  34. #&gt; 10 8 4 2
  35. #&gt; $nodes
  36. #&gt; # A tibble: 10 &#215; 1
  37. #&gt; id
  38. #&gt; &lt;dbl&gt;
  39. #&gt; 1 1
  40. #&gt; 2 2
  41. #&gt; 3 3
  42. #&gt; 4 4
  43. #&gt; 5 5
  44. #&gt; 6 6
  45. #&gt; 7 7
  46. #&gt; 8 8
  47. #&gt; 9 9
  48. #&gt; 10 10
  49. #&gt;
  50. #&gt; $edges
  51. #&gt; # A tibble: 10 &#215; 3
  52. #&gt; from to power
  53. #&gt; &lt;int&gt; &lt;int&gt; &lt;dbl&gt;
  54. #&gt; 1 1 1 10
  55. #&gt; 2 1 2 10
  56. #&gt; 3 1 3 10
  57. #&gt; 4 5 4 3
  58. #&gt; 5 2 3 3
  59. #&gt; 6 2 4 3
  60. #&gt; 7 4 1 2
  61. #&gt; 8 8 2 2
  62. #&gt; 9 8 3 2
  63. #&gt; 10 8 4 2

<sup>Created on 2023-05-14 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月14日 04:43:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定