Iterative link of tables in R

Asked

Viewed 46 times

-1

I am studying a topic of networks and need to represent all the connections between a Vertice and those connected to it, directly and indirectly, so I thought to make an iterative link between tables of the type:

inserir a descrição da imagem aqui

Be the table to be used in left_join:

ligacoes <- data.frame(origem=c("A","A", "B", "B", "D"), 
                       destino=c("B","D","C","D", "E"), 
                       valor=c(31.2, 100, 1, 85, 2))

I intended to represent all links with origin at vertex A, so I thought to perform a left Join between the table links and the table links connecting the destination column of the first with the source column of the second, as many times as the match returns results (in this sample example only 2 left Join since the 3rd left_join no longer returns results however in the dataset there may be cases where a higher left_join number is required). Can someone help me? Obg

1 answer

0

A simpler way to do what you need is to represent your data as graph. The igraph package is great for this. Expand your example to include links that do not start from A:

library(igraph)

ligacoes <- data.frame(
  origem  = c("A", "A", "B", "B", "D", "F", "G", "F"),
  destino = c("B", "D", "C", "D", "E", "G", "H", "H"),
  valor = c(31.2, 100, 1, 85, 2, 4, 7, 8))

# Converte os dados de origem e destino em objeto igraph
grafo <- graph.edgelist(as.matrix(ligacoes[1:2]))

# Inclui a variável valor como atributo das conexões:
E(grafo)$valor <- ligacoes$valor

plot(grafo)

inserir a descrição da imagem aqui

The functions *ego are used to identify the vicinity of a node. A make_ego_graph returns the neighborhoods as a list of igraph objects (as there may be more than one), preserving the attributes. Using the option order (maximum number of connections up to the given nodes) with a rather high value, will have all connections with A. The option mode = "out" indicates to follow outgoing connections.

grafoA <- make_ego_graph(grafo, order = 1e9, nodes = "A", mode = "out")[[1]]

plot(grafoA, edge.label = E(grafoA)$valor)

plot(grafoA)

inserir a descrição da imagem aqui

  • Very interesting the solution. In my case I have thousands of direct links between vertices and I need to list in a table the source vertex (e.g. A) with all vertices to which it is directly and indirectly connected, in the example that indicates would be: A-B, A-D, A-C (through B), A-E (through D)). Registering the number of connections would also be interesting as well as the weights (e.g. A-E has 2 edges and the final weight would be 100*2). I believe that the solution indicated does not allow the construction of the necessary table.

  • All this can be done by igraph, but what you’re asking is too wide for this space. First see the documentation and tutorials of the package and, if necessary, post questions about specific and specific difficulties you find. Remember that the OS is not a forum.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.