【问题标题】:Remove line in data frame if two specific characters are next to each other, R [duplicate]如果两个特定字符彼此相邻,则删除数据框中的行,R [重复]
【发布时间】:2021-06-14 13:55:27
【问题描述】:

我正在尝试删除 4 和 5 相邻的所有行,所以我只想保留具有 4 和 5 但中间至少有一个数字的行。有人知道怎么做吗?

structure(list(data_rel_1 = c("4 5 5 5", "4 5 NA NA", "4 5 5 5", 
"4 1 5 1", "4 5 5 5", "4 5 NA NA", "4 5 4 8", "4 10 5 5", "4 5 8 7"
)), row.names = c(NA, -9L), class = c("tbl_df", "tbl", "data.frame"
))

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    这行得通吗:

    library(dplyr)
    library(stringr)
    df %>% filter(str_detect(data_rel_1, '4\\s5', negate = TRUE))
    # A tibble: 2 x 1
      data_rel_1
      <chr>     
    1 4 1 5 1   
    2 4 10 5 5  
    

    【讨论】:

      【解决方案2】:

      如果只看没有4 5 是可以的,你可以使用grepldata.frame 的子集,比如:

      D[!grepl("4 5", D[[1]]),]
      #[1] "4 1 5 1"  "4 10 5 5"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-14
        • 1970-01-01
        • 1970-01-01
        • 2016-05-24
        • 1970-01-01
        • 2018-03-31
        • 1970-01-01
        • 2015-01-17
        相关资源
        最近更新 更多