简体   繁体   English

R:根据特定条件删除重复行

[英]R: Remove duplicates row based on certain criteria

I want to remove duplicate based on certain criteria.我想根据某些标准删除重复项。 My data look like:我的数据看起来像:

Animal<-c("bird","Bird ","Dog","Cat F","Lion","Lion","Lion","dog","Horse","cat", "Lion")

A_date<-c("02-08-2020","20-06-2018","01-01-2015","10-07-2021","20-06-2018","15-08-2019","05-08-2013","20-06-2010","15-11-2016","22-03-2022","15-05-2019")

ID<-c("T1", "T1","T1","T2","T2","T3","T3","T4","T4","T5","T5")

Mydata<-data.frame(Animal, A_date,col_1)

 Animal   A_date       ID
bird     02-08-2020    T1
Bird     20-06-2018    T1
Dog      01-01-2015    T1
Cat F    10-07-2021    T2
Lion     20-06-2018    T2
Lion     15-08-2019    T3
lion     05-08-2013    T3
dog      20-06-2010    T4
Horse    15-11-2016    T4
cat      22-03-2022    T5
Lion     15-05-2019    T5

I want to remove duplicate row so that only the row with latest date pr.我想删除重复的行,以便只有最新日期 pr 的行。 ID will remain. ID 将保留。 For example in the above table Lion appears 3 times with the same ID.例如在上表中 Lion 以相同的 ID 出现了 3 次。 So I want to remain with only Lion 15-08-2019 T3 but I want to keep Lion that has ID T5.所以我只想保留Lion 15-08-2019 T3 ,但我想保留 ID 为 T5 的 Lion。

The end result should be like the following:最终结果应该如下所示:

    Animal   A_date       ID
    Dog      01-01-2015    T1
    bird     02-08-2020    T1
    Dog      01-01-2015    T1
    Cat F    10-07-2021    T2
    Lion     15-08-2019    T3
    dog      20-06-2010    T4
    Horse    15-11-2016    T4
    cat      22-03-2022    T5
    Lion     15-05-2019    T5

The data that I working on is very large and has ID from T1 to T20.我处理的数据非常大,ID 从 T1 到 T20。 I have so fare tried the following code.我已经尝试了以下代码。 But it does not work properly但它不能正常工作

library(lubridate)
library(dplyr)

Mydata <- Mydata %>%
  mutate(Animal = toupper(Animal), A_date = lubridate::dmy(A_date)) %>%
  arrange(A_date)
Mydata %>%
  filter(!duplicated(Animal, fromLast = TRUE))

the result that I get我得到的结果

Animal A_date ID
DOG   <NA>    T1
HORSE <NA>    T4
BIRD  <NA>    T1
LION  <NA>    T3
BIRD  <NA>    T1
CAT F <NA>    T2
CAT   <NA>    T5

This is not the end result that i want.这不是我想要的最终结果。

One option is to group by ID and Animal , then arrange so that for each group the most recent date is at the top for that group (ie, the latest date), then slice that row.一种选择是按IDAnimal进行分组,然后进行安排,使每个组的最新日期位于该组的顶部(即最新日期),然后对该行进行slice

library(lubridate)
library(dplyr)

Mydata %>%
  mutate(Animal = trimws(toupper(Animal)), A_date = lubridate::dmy(A_date)) %>%
  group_by(ID, Animal) %>%
  arrange(ID, Animal, desc(A_date)) %>%
  slice(1)

Output Output

  Animal A_date     ID   
  <chr>  <date>     <chr>
1 BIRD   2020-08-02 T1   
2 DOG    2015-01-01 T1   
3 CAT F  2021-07-10 T2   
4 LION   2018-06-20 T2   
5 LION   2019-08-15 T3   
6 DOG    2010-06-20 T4   
7 HORSE  2016-11-15 T4   
8 CAT    2022-03-22 T5   
9 LION   2019-05-15 T5  

We can try slice_max over A_date我们可以尝试slice_max而不是A_date

Mydata %>%
    mutate(Animal = toupper(Animal), A_date = lubridate::dmy(A_date)) %>%
    group_by(ID, Animal) %>%
    slice_max(A_date) %>%
    ungroup()

which gives这使

# A tibble: 10 x 3
   Animal  A_date     ID   
   <chr>   <date>     <chr>
 1 "BIRD"  2020-08-02 T1
 2 "BIRD " 2018-06-20 T1
 3 "DOG"   2015-01-01 T1
 4 "CAT F" 2021-07-10 T2
 5 "LION"  2018-06-20 T2
 6 "LION"  2019-08-15 T3
 7 "DOG"   2010-06-20 T4
 8 "HORSE" 2016-11-15 T4
 9 "CAT"   2022-03-22 T5
10 "LION"  2019-05-15 T5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM