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

How to bind many columns to node data at once? #81

Open
alexpghayes opened this issue Dec 1, 2018 · 3 comments
Open

How to bind many columns to node data at once? #81

alexpghayes opened this issue Dec 1, 2018 · 3 comments
Labels
feature a feature request or enhancement

Comments

@alexpghayes
Copy link

So far I've got the following but I'm wondering if there's a better approach:

library(tidygraph)
#> Warning: package 'tidygraph' was built under R version 3.5.1
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#> 
#>     filter

sbm <- play_blocks(100, 100, 0.1)

node_data <- data.frame(x = rnorm(100), y = rnorm(100))

sbm %>% 
  activate(nodes) %>% 
  mutate(!!!node_data)
#> # A tbl_graph: 100 nodes and 937 edges
#> #
#> # A directed simple graph with 1 component
#> #
#> # Node Data: 100 x 2 (active)
#>        x         y
#>    <dbl>     <dbl>
#> 1 -0.123 -0.000317
#> 2  2.67   0.875   
#> 3 -1.25   1.01    
#> 4 -0.262 -1.75    
#> 5 -0.692  0.0858  
#> 6 -1.39  -0.381   
#> # ... with 94 more rows
#> #
#> # Edge Data: 937 x 2
#>    from    to
#>   <int> <int>
#> 1    11     1
#> 2    22     1
#> 3    35     1
#> # ... with 934 more rows

Created on 2018-12-01 by the reprex package (v0.2.1)

@alexpghayes
Copy link
Author

Also I'd like to export a function from a package to do something like this. Any interface or naming conventions you'd recommend following?

@jhrcook
Copy link

jhrcook commented Feb 13, 2019

I'm not entirely sure of what you are trying to do, but perhaps look into left_join? Here is how I frequently use it because my nodes will have a node attribute:

library(tidygraph)
library(tidyverse)

# make a tygr
sbm <- play_blocks(100, 100, 0.1) %N>%
    mutate(name = 1:n())
# node data
node_data <- tibble(x = rnorm(100), y = rnorm(100)) %>%
    mutate(name = 1:n())
# join to nodes
sbm <- left_join(sbm, node_data, by = "name")
sbm

#> # A tbl_graph: 100 nodes and 960 edges
#> #
#> # A directed simple graph with 1 component
#> #
#> # Node Data: 100 x 3 (active)
#>    name       x      y
#>   <int>   <dbl>  <dbl>
#> 1     1 -0.483   0.451
#> 2     2 -0.225   1.03
#> 3     3  1.67   -0.196
#> 4     4  0.0412  0.227
#> 5     5  1.04   -1.10
#> 6     6  0.588   0.955
#> # … with 94 more rows
#> #
#> # Edge Data: 960 x 2
#>    from    to
#>   <int> <int>
#> 1     3     1
#> 2     7     1
#> 3    11     1
#> # … with 957 more rows

Also look into the tidygraph::join_graph and tidygraph::bind_graph functions.

Hope this helps,
Josh Cook

@thomasp85
Copy link
Owner

The only reason why I haven't made this available is that bind_cols is not a generic... Next feature release will probably solve this somehow with bind_node_cols etc or something like that

@thomasp85 thomasp85 added the feature a feature request or enhancement label Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants