【问题标题】:Keep only non-duplicate rows based on a Column Value [duplicate]基于列值仅保留非重复行[重复]
【发布时间】:2017-12-29 14:14:51
【问题描述】:

这是对之前question 的跟进。

数据集如下所示:

dat <- read.table(header=TRUE, text="
                 ID  Veh oct nov dec jan feb
1120    1   7   47  152 259 140
2000    1   5   88  236 251 145
2000    2   14  72  263 331 147
1133    1   6   71  207 290 242
2000    3   7   47  152 259 140
2002    1   5   88  236 251 145
2006    1   14  72  263 331 147
2002    2   6   71  207 290 242
")

dat

    ID Veh oct nov dec jan feb
1 1120   1   7  47 152 259 140
2 2000   1   5  88 236 251 145
3 2000   2  14  72 263 331 147
4 1133   1   6  71 207 290 242
5 2000   3   7  47 152 259 140
6 2002   1   5  88 236 251 145
7 2006   1  14  72 263 331 147
8 2002   2   6  71 207 290 242

我喜欢根据第一列的值只保留不重复的行。输出将是这样的:

    ID Veh oct nov dec jan feb
1 1120   1   7  47 152 259 140
2 1133   1   6  71 207 290 242
3 2006   1  14  72 263 331 147

一个可能的解决方案是here。但是这个问题没有可重复的例子。

【问题讨论】:

标签: r duplicates dplyr


【解决方案1】:

我们可以使用duplicated

dat[!(duplicated(dat$ID)|duplicated(dat$ID, fromLast = TRUE)),]

【讨论】:

    猜你喜欢
    • 2019-08-12
    • 2021-08-19
    • 1970-01-01
    • 2021-09-24
    • 2018-05-08
    • 2023-03-22
    • 2016-06-22
    • 1970-01-01
    • 2017-08-01
    相关资源
    最近更新 更多