This function checks whether named lists and vectors contain invalid values (like NULL or NA), have invalid names (such as missing or empty names), ensures the number of valid names matches the number of supplied values, and confirms that valid names from the object correspond to the provided names. If any of these checks fail, the function returns the default value.

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