Search code examples
rstringdplyrfiltertidyverse

Filter rows that contain ":" in R


Given the dataframe:

df = data.frame(x = c("A:B","B:C","D","E","F"),
                y = c("1","2","3","4","5"))

How do I keep just the rows that contain ":" in column x? Normally, I would just use dplyr::filter() to delete the rows containing the string but the following code doesn't seem to work:

df %>% filter(x %in% ":")

It seems like ":" may be breaking it because it deletes all rows, but I can't seem to figure out how else to indicate ":" in R.

Edit: If there are other symbols that also trigger this issue then a general solution would also be great!


Solution

  • df %>% filter(grepl(":", x))