[R] Tidygrraph example not working

Monica Palaseanu-Lovejoy mon|c@p@|@|ovejoy @end|ng |rom gm@||@com
Thu Oct 7 15:44:27 CEST 2021


Hi,

I am trying to learn more about tidygraph, so i am following this blog:
https://www.data-imaginist.com/2017/introducing-tidygraph/

Few months ago the examples in the blog worked very well, but today they do
not.

I just installed R x64 4.1.1, so i should be up and running with the latest
on a Windows machine.

From the blog:

iris_clust <- hclust(dist(iris[1:4]))
iris_tree <- as_tbl_graph(iris_clust)

iris_tree <- iris_tree %>%
    activate(nodes) %>%
    mutate(Species = ifelse(leaf, as.character(iris$Species)[label], NA)) %>%
    activate(edges) %>%
    mutate(to_setose = .N()$Species[to] == 'setosa')
iris_tree#> # A tbl_graph: 299 nodes and 298 edges#> ##> # A rooted
tree#> ##> # Edge Data: 298 x 3 (active)#>    from    to to_setose#>
<int> <int>     <lgl>#> 1     3     1      TRUE#> 2     3     2
TRUE#> 3     7     5      TRUE#> 4     7     6      TRUE#> 5     8
4      TRUE#> 6     8     7        NA#> # ... with 292 more rows#> ##>
# Node Data: 299 x 5#>      height  leaf  label members Species#>
 <dbl> <lgl> <fctr>   <int>   <chr>#> 1 0.0000000  TRUE    108       1
 setosa#> 2 0.0000000  TRUE    131       1  setosa#> 3 0.2645751 FALSE
             2    <NA>#> # ... with 296 more rows
Now from my R session:

library(tidygraph)

Attaching package: ‘tidygraph’

The following object is masked from ‘package:stats’:

    filter
iris_clust <- hclust(dist(iris[1:4]))
iris_tree <- as_tbl_graph(iris_clust)

iris_tree <- iris_tree %>%
     activate(nodes) %>%
     mutate(Species = ifelse(leaf, as.character(iris$Species)[label], NA)) %>%
     activate(edges) %>%
     mutate(to_setose = .N()$Species[to] == "setosa")
 iris_tree
# A tbl_graph: 299 nodes and 298 edges
#
# A rooted tree
#
# Edge Data: 298 x 3 (active)
   from    to to_setose
  <int> <int> <lgl>
1     3     1 NA
2     3     2 NA
3     7     5 NA
4     7     6 NA
5     8     4 NA
6     8     7 NA
# ... with 292 more rows
#
# Node Data: 299 x 5
  height leaf  label members Species
   <dbl> <lgl> <chr>   <int> <chr>
1  0     TRUE  "108"       1 <NA>
2  0     TRUE  "131"       1 <NA>
3  0.265 FALSE ""          2 <NA>
# ... with 296 more rows

As you see the attributes from the Species column are not added to the
table, and all are NA.

It is not clear to me where i made my mistake. Any help is very much
appreciated.

Thanks,

Monica

	[[alternative HTML version deleted]]



More information about the R-help mailing list