簡體   English   中英

如何在出現此值的第一個實例的行中排除所有值? (R Dplyr)

[英]How do I exclude all values in a row where the first instance of this value occurs? (R Dplyr)

我有一個數據集,df

Subject

Hi
hello
RE: Hello
RE: How is work
No
Ok
RE: What time are 
Hello RE: are you

我想排除第一個單詞是 RE 的所有行:

主題

Hi
hello
No
Ok
Hello RE: are you

這是dput:

 structure(list(Subject = structure(c(2L, 1L, 5L, 6L, 3L, 4L, 
 7L), .Label = c("hello", "HI", "No", "ok", "RE: Hello", "RE:   How     is work", 
 "RE: What time are"), class = "factor")), class = "data.frame",       row.names = c(NA, 
 -7L))

我試過這個:

   df %>% 
   filter(Subject!= %RE:)

我不知道如何制定代碼,以便僅當是行中的第一個實例時才會排除。

你可以使用:

subset(df, !grepl('^RE', Subject))

或者使用grepinvert = TRUE

df[grep('^RE', df$Subject, invert = TRUE), , drop = FALSE]

同樣可以在dplyr實現

library(dplyr)
df %>% filter(!grepl('^RE', Subject))

slicegrep

df %>% slice(grep('^RE', Subject, invert = TRUE))

另一種解決方案

library(stringr)
library(tidyverse)
df %>% 
  filter(str_detect(Subject, pattern = "^[RE]", negate = T))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM