Lynn
Lynn

Reputation: 4408

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

I have a dataset, df

Subject

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

I would like to exclude all rows where the first word is RE:

Subject

Hi
hello
No
Ok
Hello RE: are you

Here is the 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))

I have tried this:

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

I do not know how to formulate the code so that it will exclude only if is the first instance within the row.

Upvotes: 0

Views: 108

Answers (2)

Yuriy Saraykin
Yuriy Saraykin

Reputation: 8880

another solution

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

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389335

You could use :

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

Or with grep and invert = TRUE

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

Same can be implemented in dplyr as well

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

and with slice and grep

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

Upvotes: 2

Related Questions