【问题标题】:Delete rows that are repeated for only some of the columns of the dataframe [duplicate]删除仅对数据框的某些列重复的行[重复]
【发布时间】:2021-01-28 06:50:07
【问题描述】:

将示例数据框视为:

df <- data.frame(a=c(rep(1,4),4,7,8), b=c(rep(4,4),6,8,3), 
                 c=c(rep("hey",4),"hi","hello","salam"), 
                 d=c("q","r","g","y","d","e","y"), e=c(2,6,43,56,6,23,4))

我想删除与列 abc 相同的行。所需的输出将是三行

    a b     c     d   e
1   1 4    hey    q   2 
5   4 6    hi     d   6
6   7 8   hello   e   23
7   8 3   salam   y   4

【问题讨论】:

  • 你也可以df %&gt;% group_by(a, b, c) %&gt;% filter(row_number() == 1)

标签: r dataframe


【解决方案1】:

我想你忘记了第一行

df[!duplicated(df[,c("a","b","c")]),]

  a b     c d  e
1 1 4   hey q  2
5 4 6    hi d  6
6 7 8 hello e 23
7 8 3 salam y  4

【讨论】:

  • 是的,你是对的!我会更新的!
【解决方案2】:

dplyr解决办法是:

library(dplyr)
df %>% distinct(a, b, c, .keep_all = TRUE)

【讨论】:

    猜你喜欢
    • 2011-10-10
    • 2021-05-14
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多