【问题标题】:How to drop remove rows from tibble based on multiple column values( duplicate and string value)如何根据多列值(重复值和字符串值)从 tibble 中删除行
【发布时间】:2021-02-16 03:18:14
【问题描述】:

我不知道该怎么做。在下面的小标题中,我想根据几件事删除第 4 行。

  1. 型号 == "EADS142"
  2. 重复属性 BC02S 出现在第 3 行,型号 =="EADS14" 我不想删除第 2 行,尽管 models=="EADS142" 并在第 1 行重复属性,因为在第 1 行模型!="EADS14"

``

 filtered
# A tibble: 7 x 2
attributes models 
<chr>        <chr>   
1 AGG413.    EADS05
2 AGG413     EADS142
3 BC02S      EADS14
4 BC02S      EADS142

Expected result
# A tibble: 4 x 2
attributes models 
<chr>      <chr>  
1 AGG413     EADS05 
2 AGG413     EADS142 
3 BC02S      EADS14 

【问题讨论】:

    标签: r dplyr tidyr


    【解决方案1】:

    duplicatedlag 一起使用:

    library(dplyr)
    df %>% 
      filter(!(duplicated(attributes) & 
               models == 'EADS142' & lag(models) == 'EADS14'))
    
    #  attributes  models
    #1    AGG413.  EADS05
    #2     AGG413 EADS142
    #3      BC02S  EADS14
    

    数据

    df <- structure(list(attributes = c("AGG413.", "AGG413", "BC02S", "BC02S"
    ), models = c("EADS05", "EADS142", "EADS14", "EADS142")), 
    class = "data.frame", row.names = c(NA, -4L))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-29
      • 2023-03-10
      • 2019-12-15
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多