Home > Blockchain >  Filter rows that contain ":" in R
Filter rows that contain ":" in R

Time:11-27

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!

CodePudding user response:

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

CodePudding user response:

In base R: We could add the logic to the row parameter in the brackets:

df[grepl(':',df$x),]
  x y
1 A:B 1
2 B:C 2
  • Related