如何基于多列删除非唯一行

时间:2015-04-09 23:47:52

标签: r

我有一个数据框:

data.frame(name= c("b","d","d","e"),date=as.Date(c("2015-01-01","2015-01-02","2015-02-02","2015-03-02")),s=c("e","b","b","g"),num=c(1,2,2,3))

  name     date   s  num
1    b 2015-01-01 e   1
2    d 2015-01-02 b   2
3    d 2015-02-02 b   2
4    e 2015-03-02 g   3

我希望删除不唯一的行,所以在上面的情况下我想删除行2& 3因为namedates& num。所以结果将是:

 name     date   s  num
1    b 2015-01-01 e   1
2    e 2015-03-02 g   3

任何想法?

1 个答案:

答案 0 :(得分:1)

如果我理解正确,我们必须删除所有被视为重复的行

df1[!duplicated(df1) & !duplicated(df1, fromLast = TRUE) ,]

使用数据:

df1  <- 
  data.frame(
    name= c("b","d","d","e"),
    date=as.Date(c("2015-01-01","2015-01-02","2015-01-02","2015-03-02")),
    s=c("e","b","b","g"),
    num=c(1,2,2,3))

结果是

#   name       date s num
# 1    b 2015-01-01 e   1
# 4    e 2015-03-02 g   3