Regular expressions (RegEx) and dplyr::filter()

前端 未结 2 1570
情话喂你
情话喂你 2020-12-15 16:50

I have a simple data frame that looks like this:

x <- c(\"aa\", \"aa\", \"aa\", \"bb\", \"cc\", \"cc\", \"cc\")
y <- c(101, 102, 113, 201, 202, 344, 40         


        
相关标签:
2条回答
  • 2020-12-15 17:29

    You need to double check the documentations for grepl and filter.

    For grep/grepl you have to also supply the vector that you want to check in (y in this case) and filter takes a logical vector (i.e. you need to use grepl). If you want to supply an index vector (from grep) you can use slice instead.

    df %>% filter(!grepl("^1", y))
    

    Or with an index derived from grep:

    df %>% slice(grep("^1", y, invert = TRUE))
    

    But you can also just use substr because you are only interested in the first character:

    df %>% filter(substr(y, 1, 1) != 1)
    
    0 讨论(0)
  • 2020-12-15 17:31

    With a combination of dplyrand stringr (to stay within the tidyverse), you could do :

    df %>% filter(!str_detect(y, "^1"))
    

    This works because str_detect returns a logical vector.

    0 讨论(0)
提交回复
热议问题