Replace values if NULL, NA, blank after trimming, or an empty list

if_nowt(x, replace = "", test_for = c("null", "na", "trim", "empty"))

Arguments

x

vector or list to test

replace

value to replace with

test_for

values to test for ("null" replaces NULL values, "na", replaces NA values, "trim" replaces empty strings after trimws(), "empty" replaces empty lists)

Value

vector or list with replaced values

Examples

if_nowt(NULL)
#> [1] ""
if_nowt(NA)
#> [1] ""
if_nowt("   ")
#> [1] ""
if_nowt(c(1, 2, NA), replace = 0)
#> [1] 1 2 0
x <- list(NULL, NA, " ", list())
if_nowt(x) %>% str()
#> List of 4
#>  $ : chr ""
#>  $ : chr ""
#>  $ : chr ""
#>  $ : chr ""
if_nowt(x, test_for = "null") %>% str()
#> List of 4
#>  $ : chr ""
#>  $ : logi NA
#>  $ : chr " "
#>  $ : list()
if_nowt(x, test_for = "na") %>% str()
#> List of 4
#>  $ : NULL
#>  $ : chr ""
#>  $ : chr " "
#>  $ : list()
if_nowt(x, test_for = "trim") %>% str()
#> List of 4
#>  $ : NULL
#>  $ : logi NA
#>  $ : chr ""
#>  $ : list()
if_nowt(x, test_for = "empty") %>% str()
#> List of 4
#>  $ : NULL
#>  $ : logi NA
#>  $ : chr " "
#>  $ : chr ""