簡體   English   中英

R:根據特定條件刪除重復行

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

我想根據某些標准刪除重復項。 我的數據看起來像:

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

我想刪除重復的行,以便只有最新日期 pr 的行。 ID 將保留。 例如在上表中 Lion 以相同的 ID 出現了 3 次。 所以我只想保留Lion 15-08-2019 T3 ,但我想保留 ID 為 T5 的 Lion。

最終結果應該如下所示:

    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

我處理的數據非常大,ID 從 T1 到 T20。 我已經嘗試了以下代碼。 但它不能正常工作

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))

我得到的結果

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

這不是我想要的最終結果。

一種選擇是按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

  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  

我們可以嘗試slice_max而不是A_date

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

這使

# 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