R - How to test for character(0) in IF statement

后端 未结 6 1937
别那么骄傲
别那么骄傲 2020-12-14 14:36

I imagine this is incredibly simple but I cant seem to find the answer.

I am writing an IF statement but the test is if the object returns a character(0)

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 14:54

    Many people forget that functions like str_locate_all() require %>% unlist() or %>% .[[1]].

    Then you can easily detect character(0) with the length() function if > 0 one can safely use the output of str_locate_all() for example.

    Example:

    value <- str_extract_all("kvk :", ascii_digit(8,8)) %>% unlist()
    if (length(value) > 0) {
      # Do something
    }
    

提交回复
热议问题