【问题标题】:Unique combinations of two vectors without pair repetition [duplicate]没有对重复的两个向量的唯一组合[重复]
【发布时间】:2015-08-17 05:24:13
【问题描述】:

我有一个问题,我需要保留唯一的行但没有重复的对,例如如果我有一张 x1=c(1,2,3) 和 x2=c(2,1,3) 的表格,则只会保留第一行和第三行。带有 (2,1) 的行被丢弃,因为 (1,2) 已经存在。有人可以帮忙吗?

谢谢!

【问题讨论】:

    标签: r


    【解决方案1】:

    我们可以使用apply逐行sort,使用duplicated获取逻辑索引来删除重复的行。

     df1[!duplicated(t(apply(df1, 1, sort))),]
     #   x1 x2
     #1  1  2
     #2  1  3
     #3  1  1
     #4  2  4
     #6  2  2
     #7  3  4
     #8  3  2
    

    数据

    df1 <- structure(list(x1 = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
    4L), x2 = c(2L, 3L, 1L, 4L, 1L, 2L, 4L, 2L, 1L, 2L, 2L)), .Names = c("x1", 
    "x2"), class = "data.frame", row.names = c(NA, -11L))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多