Skip to contents

This function assesses whether named lists and vectors have invalid values (like NULL or NA), invalid names (such as missing or empty names), confirms that the count of valid names matches the count of provided values, and verifies that the valid names obtained from the named object align with the supplied names. If any checks fail, the default value is returned.

Usage

check_named_vctr(x, names, default)

Arguments

x

A named vector.

names

A character vector or list of character vectors of length one specifying the names to be matched.

default

Default value to return

Value

Either the original object, x, or the default value.

Author

Ama Nyame-Mensah

Examples


# returns NULL
check_named_vctr(x = c(one = 1, two = 2, 3), 
                 names = c("one", "two", "three"),
                 default = NULL)
#> NULL
                 
# returns x
check_named_vctr(x = list(one = 1, two = 2, three = 3), 
                 names = list("one", "two", "three"),
                 default = NULL)  
#> $one
#> [1] 1
#> 
#> $two
#> [1] 2
#> 
#> $three
#> [1] 3
#> 

# also returns x
check_named_vctr(x = c(baako = 1, mmienu = 2, mmiensa = 3), 
                 names = list("baako", "mmienu", "mmiensa"),
                 default = NULL)              
#>   baako  mmienu mmiensa 
#>       1       2       3