如何打印tbl_graph对象的更多行

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

How to print more lines of a tbl_graph object

问题

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

I have the following tidy graph

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

the output is the following

graph
# A tbl_graph: 10 nodes and 10 edges
#
# A directed multigraph with 5 components
#
# A tibble: 10 × 1
     id
  <dbl>
1     1
2     2
3     3
4     4
5     5
6     6
# ℹ 4 more rows
# ℹ Use `print(n = ...)` to see more rows
#
# A tibble: 10 × 3
   from    to power
  <int> <int> <dbl>
1     1     1    10
2     1     2    10
3     1     3    10
# ℹ 7 more rows
# ℹ 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 来展示所有行,代码示例如下:

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

library(tidygraph)
library(tibble)
mynodes &lt;- tibble(id = c(1,2,3,4,5,6,7,8,9,10))
myedges &lt;- tibble(from = c(1,1,1,5,2,2,4,8,8,8),
                  to = c(1,2,3,4,3,4,1,2,3,4),
                  power = c(10,10,10,3,3,3,2,2,2,2))
graph = tbl_graph(nodes = mynodes, edges = myedges)
lapply(graph, \(x) x %&gt;% print(n = nrow(.)))
#&gt; # A tibble: 10 &#215; 1
#&gt;       id
#&gt;    &lt;dbl&gt;
#&gt;  1     1
#&gt;  2     2
#&gt;  3     3
#&gt;  4     4
#&gt;  5     5
#&gt;  6     6
#&gt;  7     7
#&gt;  8     8
#&gt;  9     9
#&gt; 10    10
#&gt; # A tibble: 10 &#215; 3
#&gt;     from    to power
#&gt;    &lt;int&gt; &lt;int&gt; &lt;dbl&gt;
#&gt;  1     1     1    10
#&gt;  2     1     2    10
#&gt;  3     1     3    10
#&gt;  4     5     4     3
#&gt;  5     2     3     3
#&gt;  6     2     4     3
#&gt;  7     4     1     2
#&gt;  8     8     2     2
#&gt;  9     8     3     2
#&gt; 10     8     4     2
#&gt; $nodes
#&gt; # A tibble: 10 &#215; 1
#&gt;       id
#&gt;    &lt;dbl&gt;
#&gt;  1     1
#&gt;  2     2
#&gt;  3     3
#&gt;  4     4
#&gt;  5     5
#&gt;  6     6
#&gt;  7     7
#&gt;  8     8
#&gt;  9     9
#&gt; 10    10
#&gt; 
#&gt; $edges
#&gt; # A tibble: 10 &#215; 3
#&gt;     from    to power
#&gt;    &lt;int&gt; &lt;int&gt; &lt;dbl&gt;
#&gt;  1     1     1    10
#&gt;  2     1     2    10
#&gt;  3     1     3    10
#&gt;  4     5     4     3
#&gt;  5     2     3     3
#&gt;  6     2     4     3
#&gt;  7     4     1     2
#&gt;  8     8     2     2
#&gt;  9     8     3     2
#&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:

确定