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

后端 未结 6 1921
别那么骄傲
别那么骄傲 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条回答
  •  萌比男神i
    2020-12-14 14:54

    Adding the obligatory tidyverse answer. The rlang package has the function is_empty(), which does exactly what you want.

    Test <- character(0)
    rlang::is_empty(Test)
    #[1] TRUE
    

    This also works for empty vectors that aren't characters. For example, it works in the case that Patrick Roocks describes in comments.

    Test <- as.Date(character(0))
    rlang::is_empty(Test)
    #[1] TRUE
    

    Loading the 'tidyverse' package also loads is_empty().

提交回复
热议问题