简体   繁体   中英

Applying combine function over dataframe and saving as a single list/dataframe

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.

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) })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM