Reputation: 97
I am trying to create a network. I have a dataframe:
. . v1 .v2 .v3
1/ .1 ... 2.... 3
2/ .3 ... 4 .. 7
3/. 6 ...11 . 9
I wish to find all possible combinations within each row. For example, the 1st row has the values 1, 2, 3 so the result would be (1, 2) (1, 3) (2, 3). Then I would go on to find the combinations between values in only the second row 3, 4, 7 returning (3, 4) (3, 7) (4, 7).
Now I wish to do this function by row and then combine all the results to have an edge list of sorts to create the network out of.
I have tried for couple hours iterations of for functions, apply, and combn but I cant seem to get it to work.
Does anyone have any ideas on how to approach this?
Finally, I am relatively new to R. Is there a way I should break it down or do something step by step to solve? I would really appreciate if you could use simple functions like apply, for loops, and stuff rather than odd specific functions. That way I can look over your ideas and learn from them so I can apply them to other problems later.
Upvotes: 0
Views: 47
Reputation: 5059
You were probably very close
(though you should post the code of your attempt in the future,
and also use something like dput
for your data).
The problem of using apply
is that it will try to simplify automatically
(to something like a single vector or matrix),
and since combn
returns matrices,
you definitely don't want simplification.
You should use lapply
when you have cases like these:
lapply(1L:nrow(your_data_frame), function(i) { combn(your_data_frame[i,], m=2L) })
Upvotes: 2