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

前端 未结 2 1569
情话喂你
情话喂你 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)
    

提交回复
热议问题