Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot create data frame column for tbl_graph class #160

Closed
JosiahParry opened this issue Aug 16, 2022 · 5 comments
Closed

Cannot create data frame column for tbl_graph class #160

JosiahParry opened this issue Aug 16, 2022 · 5 comments

Comments

@JosiahParry
Copy link

With an object of class tbl_graph it is not possible to create a data frame column. However, it is possible to do so with a normal data frame. This would be a welcomed addition to tidygraph functionality.

library(dplyr)
library(tidygraph)

nn <- create_star(10, directed = TRUE, mutual = TRUE)

# this should work
nn |> 
  mutate(id = row_number(),
         df = .N())
#> Error in `vertex.attributes<-`(graph, index = index, value = value): Invalid attribute value length, must match number of vertices

# cast as data.frame
nn_df <- nn |> 
  mutate(id = row_number()) |> 
  as_tibble()

# create data.frame column
nn_df |> 
  mutate(df = nn_df)
#> # A tibble: 10 × 2
#>       id df$id
#>    <int> <int>
#>  1     1     1
#>  2     2     2
#>  3     3     3
#>  4     4     4
#>  5     5     5
#>  6     6     6
#>  7     7     7
#>  8     8     8
#>  9     9     9
#> 10    10    10

Created on 2022-08-16 by the reprex package (v2.0.1)

@thomasp85
Copy link
Owner

This is sadly a limitation of igraph which is used as the internal data structure. I'm not sure they'd be willing to add this complexity to the code base. What say you @gaborcsardi?

@gaborcsardi
Copy link

I am not sure I follow. nn is not a data frame, so you cannot call mutate() on it. Are you saying that a graph should be a data frame?

@thomasp85
Copy link
Owner

This is tidygraph syntax... but the underlying limitation described here is that igraph node and edge attributes cannot be a dataframe

@gaborcsardi
Copy link

I don't remember such limitation, I thought they could be generic R objects:

library(igraph)
g <- make_star(10)
va <- replicate(10, mtcars, simplify = FALSE)
V(g)$foo  <- va
g
#> IGRAPH ff30718 D--- 10 9 -- In-star
#> + attr: name (g/c), mode (g/c), center (g/n), foo (v/x)
#> + edges from ff30718:
#> [1]  2->1  3->1  4->1  5->1  6->1  7->1  8->1  9->1 10->1

@thomasp85
Copy link
Owner

They can be lists, but they can't be a data.frame with the rows in the data frame corresponding to the vertices/edges

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants