Remove elements based on element content?


bad thing
df.cleaned <- df[-which(str_detect(df, "Not found")),]

"df" refers to a data frame consisting of multiple columns and rows. Many elements in this data frame have certain words in them.

What I'm trying to do is remove all values ​​that contain the word "not found", either as a whole element value or as part of an element value.

So far the above command is what I came up with using the stringr package. However, this command seems to delete the entire line. I don't want to delete the whole row, I just want to delete the specific element that contains "not found".

MKR

Your idea is correct. You need to try to apply it to each item. One option is to use sapply. Check each item str_detectand replace with "", NAotherwise return the value of the item.

library(stringr)
df.clean <- as.dataframe(sapply(df, 
                   function(x)ifelse(str_detect(x, "Not found"), "",x)))
df.clean
# 
# A    B
# 1  A Good
# 2  B     
# 3  C Good
# 4  D     
# 5  E Good
# 6  A     
# 7  B Good
# 8  C     
# 9  D Good
# 10 E   

data

    df <- data.frame(A = rep(c("A", "B", "C", "D", "E"), 2), 
                     B = rep(c("Good","Bad with Not found"),5),
                      stringsAsFactors = FALSE)
df
# A                  B
# 1  A               Good
# 2  B Bad with Not found
# 3  C               Good
# 4  D Bad with Not found
# 5  E               Good
# 6  A Bad with Not found
# 7  B               Good
# 8  C Bad with Not found
# 9  D               Good
# 10 E Bad with Not found

Related


How to remove specific element with content based on position

Rudramuni TP For suggestions on how to remove "aaaaa" "mi" contained in the text , If found under msub's first child and last position (text node) If found under the second child of msub and the first position (text node) For a quick look, I've added comments

Remove elements based on element content?

bad thing df.cleaned <- df[-which(str_detect(df, "Not found")),] "df" refers to a data frame consisting of multiple columns and rows. Many elements in this data frame have certain words in them. What I'm trying to do is remove all values that contain the word

Remove elements based on element content?

bad thing df.cleaned <- df[-which(str_detect(df, "Not found")),] "df" refers to a data frame consisting of multiple columns and rows. Many elements in this data frame have certain words in them. What I'm trying to do is remove all values that contain the word

Remove elements based on element content?

bad thing df.cleaned <- df[-which(str_detect(df, "Not found")),] "df" refers to a data frame consisting of multiple columns and rows. Many elements in this data frame have certain words in them. What I'm trying to do is remove all values that contain the word

Remove elements based on element content?

bad thing df.cleaned <- df[-which(str_detect(df, "Not found")),] "df" refers to a data frame consisting of multiple columns and rows. Many elements in this data frame have certain words in them. What I'm trying to do is remove all values that contain the word

Remove elements based on element content?

bad thing df.cleaned <- df[-which(str_detect(df, "Not found")),] "df" refers to a data frame consisting of multiple columns and rows. Many elements in this data frame have certain words in them. What I'm trying to do is remove all values that contain the word

How to remove specific element with content based on position

Rudramuni TP For suggestions on how to remove "aaaaa" "mi" contained in the text , If found under msub's first child and last position (text node) If found under the second child of msub and the first position (text node) For a quick look, I've added comments

Remove elements based on element content?

bad thing df.cleaned <- df[-which(str_detect(df, "Not found")),] "df" refers to a data frame consisting of multiple columns and rows. Many elements in this data frame have certain words in them. What I want to do is remove all values that contain the word "no

Remove elements based on element content?

bad thing df.cleaned <- df[-which(str_detect(df, "Not found")),] "df" refers to a data frame consisting of multiple columns and rows. Many elements in this data frame have certain words in them. What I want to do is remove all values that contain the word "no

Remove elements based on element content?

bad thing df.cleaned <- df[-which(str_detect(df, "Not found")),] "df" refers to a data frame consisting of multiple columns and rows. Many elements in this data frame have certain words in them. What I want to do is remove all values that contain the word "no

Remove elements based on element content?

bad thing df.cleaned <- df[-which(str_detect(df, "Not found")),] "df" refers to a data frame consisting of multiple columns and rows. Many elements in this data frame have certain words in them. What I want to do is remove all values that contain the word "no

Remove elements based on element content?

bad thing df.cleaned <- df[-which(str_detect(df, "Not found")),] "df" refers to a data frame consisting of multiple columns and rows. Many elements in this data frame have certain words in them. What I want to do is remove all values that contain the word "no

Remove elements based on the content of other elements

shadow I've done quite a few very jQuery scripts with slight modifications to suit my needs, but I've never written any scripts before at all. Now I find myself completely overwhelmed, so I'm really hoping someone can help me. I have the following script to cr