0

I have a data frame containing three columns and first column is Species_Name which contain all species name but i want to remove those rows which are undetermined like "Salmonella sp" and want to keep only those rows which have full determined name like Salmonella enterica or bongori and so on. I tried following code but its not working. please give any suggestions.

dfcox1 <- dfcox1 %>%
filter(Species_Name != "Salmonella sp")
noor
  • 11
  • 3
  • 1
    Please provide enough data to reproduce the issue. You can do `dput(dfcox1)`, or, if it's a large data frame, just enough rows to show where `filter` is failing. – andrew_reece Dec 20 '20 at 23:28
  • Try : `dfcox1 %>% filter(trimws(Species_Name) != "Salmonella sp")` – Ronak Shah Dec 21 '20 at 03:17

1 Answers1

0

Welcome on stackoverflow.com! Please create reproducible examples so that other people have it easier to help you, which is especially easy when working with GNU R.

If you want to remove a row in a dataframe according to a specific regular expression (e.g. the row name ending with sp), you can do so as follows):

iris %>%
  dplyr::filter(!stringr::str_detect(Species, "sp"))

n0542344
  • 111
  • 4
  • note that with `str_detect` you can set `negate = TRUE` instead of negation with `!`. even so, i'm not sure this totally answers the question of why OP's solution didn't work. i've encountered the same issue before, and i also resort to `str_detect`, but it doesn't always seem necessary. it'd be interesting to see OP's actual `dfcox1`, to better diagnose what is actually going wrong. – andrew_reece Dec 21 '20 at 01:38
  • 1
    @andrew_reece: good point, I didn't think about `negate = TRUE`. Concerning the necessity of the `str_detect`-string: maybe it would be better to specify the regex directly (e.g. by writing `sp$` so that the `sp`-part has to be at the end of the text). – n0542344 Dec 21 '20 at 22:48