【问题标题】:Remove duplicate combinations in R [duplicate]删除 R 中的重复组合 [重复]
【发布时间】:2018-09-02 09:57:12
【问题描述】:

我正在使用 R,我想删除 data.frame 中的组合。独特的功能似乎无法胜任。

  a b c
1 1 4 A
2 2 3 B
3 1 5 C
4 4 1 A
5 5 1 C
6 3 2 B
7 3 2 E

我想得到一个看起来像这样的东西,只保留 a 列和 b 列的一个组合(c 列是频率度量):

       a b c
     1 1 4 A
     2 2 3 B
     3 1 5 C

非常感谢!

PS:问题的根源是 dcast 函数返回此错误: 缺少聚合函数:默认为长度 (R reshape2 'Aggregation function missing: defaulting to length')

【问题讨论】:

标签: r duplicates unique combinations


【解决方案1】:
df[!duplicated(t(apply(df[c("a", "b")], 1, sort))), ]
  a b c
1 1 4 A
2 2 3 B
3 1 5 C

地点:

df <- data.frame(
  a = c(1L, 2L, 1L, 4L, 5L, 3L, 3L), 
  b = c(4L, 3L, 5L, 1L, 1L, 2L, 2L), 
  c = c("A", "B", "C", "A", "C", "B", "E")
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 2015-01-19
    • 1970-01-01
    • 2019-08-08
    • 2017-01-31
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    相关资源
    最近更新 更多