简体   繁体   English

在数据框上应用合并功能并保存为单个列表/数据框

[英]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 v1 .v2 .v3
1/ .1 ... 2.... 3 1 / .1 ... 2 .... 3

2/ .3 ... 4 .. 7 2 / .3 ... 4 .. 7

3/. 3 /。 6 ...11 . 6 ... 11。 9 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). 例如,第一行的值为1、2、3,因此结果将是(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). 然后,我将继续查找仅在第二行3、4、7返回(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? 最后,我是R的新手。是否有办法将其分解或逐步解决? I would really appreciate if you could use simple functions like apply, for loops, and stuff rather than odd specific functions. 如果可以使用诸如apply,for循环和填充之类的简单功能,而不是使用奇特的特定功能,我将不胜感激。 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). 您可能已经很接近了(尽管您将来应该发布尝试的代码,并且还使用dput的数据)。

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. 使用apply的问题是它将尝试自动进行简化(简化为单个向量或矩阵),并且由于combn返回矩阵,因此您绝对不希望进行简化。 You should use lapply when you have cases like these: lapply时,应使用lapply

lapply(1L:nrow(your_data_frame), function(i) { combn(your_data_frame[i,], m=2L) })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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