将三份重复拆分为重复份 [英] Splitting triplicates into duplicates

查看:80
本文介绍了将三份重复拆分为重复份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一式三份的结果集分成三个可能的重复项。

I am wanting to split sets of triplicate results into the three possible duplicates.

如果初始集合为

    A   B   C
1 122 106 114
2 110 122 110

我想将其转换为

    A   B
1 122 106
2 122 114
3 106 114
4 110 122
5 110 110
6 122 110

combin函数将逐行完成此操作,但我无法弄清楚如何将其应用于整个数据帧(这可能会很大,我仅使用两行进行演示。)。

The combn function will do it line by line but I cannot figure out how to apply it across the full dataframe (which can be quite large, I only used two lines for demonstration purposes.).

推荐答案

您可以执行以下操作:


  1. 使用 lapply()对数据的每一行应用您的 combn

  2. 使用 rbind 合并结果

  3. 利用 do.call的魔力组合由步骤1和2创建的列表。

  1. Use lapply() to apply your combn for each row of the data
  2. Use rbind to combine the results
  3. Make use of the magic of do.call to combine the list created by steps 1 and 2.

在一行代码:

do.call(rbind, lapply(seq(nrow(dat)), function(i)t(combn(dat[i, ], 2))))
    [,1] [,2]
[1,] 122  106 
[2,] 122  114 
[3,] 106  114 
[4,] 110  122 
[5,] 110  110 
[6,] 122  110 

这篇关于将三份重复拆分为重复份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆