0

I tried finding a solution, but since I am not that familiar with R, I'm not sure if I used the best key words searching.

I have an igraph where vertices have attributes(positions, wealth) and I'm trying to compare the wealth of those vertices that have positions == "Manager".

Edit

I'm not only comparing the wealth but also another attribute: constraint. Also I tried to do the make this reproducible:

library(igraph)  
M <- matrix(c( 0, 1, 0, 0, 0,   
           0, 0, 1, 0, 0,   
           1, 1, 0, 0, 1,   
           0, 1, 0, 0, 0,   
           0, 1, 1, 0, 0), nrow = 5, byrow=TRUE)                   
g <- graph.adjacency(M, mode = "undirected")  


V(g)$position <- c("Manager", "Manager", "Other", "Other", "Other")  
V(g)$wealth <- c("12", "16", "16", "4", "29")  
V(g)$constraint <- constraint(g)  

What I want to do is to see a table with the wealth and constraint of the Managers only.

Edit 2

@G5W offered this solution which works perfectly:

cbind(V(g)$wealth, V(g)$constraint)[V(g)$position == "Manager"]
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    We indeed need that: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. At the very least it will increase your chances to get an (good) answer. An extra question would be: how do you want to compare this wealth? Or perhaps what you want our help with is just extracting these values and you are going to do the rest? – Julius Vainora Feb 26 '18 at 22:23
  • https://github.com/thomasp85/tidygraph – RobertMyles Feb 26 '18 at 22:32
  • What do you mean `I'm trying to compare the wealth of those vertices that have positions == "Manager"`? Do you want a summary of the distribution of wealth? – G5W Feb 26 '18 at 23:55
  • @Julius thank you for that link. I edited the original question. Afterwards I want to see if there is a correlation, but for now I just want to get an overview – uschi deinemudda Feb 26 '18 at 23:58
  • @G5W Sorry, that wasn't very precise of me. I think the best would be a table – uschi deinemudda Feb 27 '18 at 00:02
  • Does `cbind(V(g)$wealth, V(g)$constraint)[V(g)$position == "Manager",]` meet your needs? – G5W Feb 27 '18 at 00:09
  • @G5W It does! Thank you so much – uschi deinemudda Feb 27 '18 at 00:24

1 Answers1

0

I think I understand what you're asking. For this sort of thing, I prefer to use the dplyr package (as part of the tidyverse) because it is usually followed with further wrangling.

Let's say that your data is stored in the dataframe df. We can then do the following:

df %>%
filter(position == "Manager")

This returns all Manager entries.

Alternatively, using the base package, you can use

df[df$position == "Manager",]

I should add that I'm not familiar with igraph and so for a better answer, sample data should be provided.

Daniel V
  • 1,305
  • 7
  • 23