Reputation: 55
I have a table of 'positive' cases for a variable in a household. I want to eventually output a network diagram in iGraph using R. I want to represent each household as a network and therefore represent those who are positive and negative in each household. My input dataframe is as follows:
ID | Result | Household | Number in household |
---|---|---|---|
A | Positive | H1 | 2 |
B | Positive | H1 | 2 |
C | Positive | H2 | 3 |
D | Positive | H3 | 2 |
My first requirement is to expand a the list to the number of variables in the household, to result with:
ID | Result | Household | Number in household |
---|---|---|---|
A | Positive | H1 | 2 |
B | Positive | H1 | 2 |
C | Positive | H2 | 3 |
C1 | Negative | H2 | 2 |
D | Positive | H3 | 3 |
D1 | Negative | H3 | 3 |
D2 | Negative | H3 | 3 |
From this point I need a dataframe which represents the network edge list, i.e. if one person has an interaction (both directions) with another represented by their household. This would look like:
ID | Interaction | Household |
---|---|---|
A | B | H1 |
B | A | H1 |
C | C1 | H2 |
C1 | C | H2 |
D | D1 | H3 |
D | D2 | H3 |
D1 | D | H3 |
D1 | D2 | H3 |
D2 | D | H3 |
D2 | D1 | H3 |
This edge list would then be inputted alongside the 'attributes' list of positive and negative results, which can then be used to build the network. I'm just struggling to get the data in the correct format to start!
I am a bit of a rookie in R and stack overflow - I hope the above makes sense and happy to clarify!
Upvotes: 1
Views: 83
Reputation: 102519
I don't think you need igraph
to reach your goal. Below is a data.table
option
> setDT(df)[, CJ(ID, Interaction = ID), Household][ID != Interaction, c(2, 3, 1)]
ID Interaction Household
1: A B H1
2: B A H1
3: C C1 H2
4: C1 C H2
5: D D1 H3
6: D D2 H3
7: D1 D H3
8: D1 D2 H3
9: D2 D H3
10: D2 D1 H3
Upvotes: 2