If I have this dataframe
prove<- data.frame(a=c(1,2,3),
b=c("test_4", "test_4fail", "test_5"),
c=c(8,4,5))
What is the most efficient way to delete rows where in column b, there is the word "Fail"
If I have this dataframe
prove<- data.frame(a=c(1,2,3),
b=c("test_4", "test_4fail", "test_5"),
c=c(8,4,5))
What is the most efficient way to delete rows where in column b, there is the word "Fail"
You could use a combination of dplyr::filter()
and stringr::str_detect()
. Note, I use the negate = TRUE
argument of str_detect()
to indicate I want to keep rows where "fail"
does not appear.
library(tidyverse)
prove <- data.frame(
a = c(1, 2, 3),
b = c("test_4", "test_4fail", "test_5"),
c = c(8, 4, 5)
)
prove %>%
filter(str_detect(b, "fail", negate = TRUE))
#> a b c
#> 1 1 test_4 8
#> 2 3 test_5 5
Created on 2019-02-20 by the reprex package (v0.2.1)
i have this ERROR
Error in filter_impl(.data, quo) :
Evaluation error: unused argument (negate = T).
Looks like you have an old version of stringr
installed. The negate
argument was added in version 1.4.0. You could update to the latest version of stringr
by running:
install.packages("stringr")
Alternatively, you could use !
to negate str_detect()
like so:
library(tidyverse)
prove <- data.frame(
a = c(1, 2, 3),
b = c("test_4", "test_4fail", "test_5"),
c = c(8, 4, 5)
)
prove %>%
filter(!str_detect(b, "fail"))
#> a b c
#> 1 1 test_4 8
#> 2 3 test_5 5
Created on 2019-02-20 by the reprex package (v0.2.1)
This question and your previous questions are highly correlated with patterns in strings, so this information could be very useful for you to learn more about this topic.
https://stringr.tidyverse.org/articles/regular-expressions.html
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.