which.min.key takes either a character vector or a list of strings and returns the location of the element that is lexicographically (using bytewise comparison) the first. In a sense it is which.min for strings. In addition, it supports prefix comparisons using a key delimiter (see below).

which.min.key(keys, sep = "|")

Arguments

keys

character vector or a list of strings to use as input

sep

string specifying the key delimiter. Only the first character is used. Can be "" if the entire string is to be treated as a key.

Details

which.min.key considers the prefix of each element in keys up to the delimiter specified by sep. It returns the index of the element which is lexicographically first among all the elements, using bytewise comparison (i.e. the locale is not used and multi-byte characters are not considered as one character).

If keys is a character vector then NA elements are treated as non-existent and will never be picked.

If keys is a list then only string elements of length > 0 are eligible and NAs are not treated specially (hence they will be sorted in just like the "NA" string).

Value

scalar integer denoting the index of the lexicographically first element. In case of a tie the lowest index is returned. If there are no eligible elements in keys then a zero-length integer vector is returned.

Author

Simon Urbanek

See also

Examples

which.min.key(c("g","a","b",NA,"z","a"))
#> [1] 2
which.min.key(c("g","a|z","b",NA,"z|0","a"))
#> [1] 2
which.min.key(c("g","a|z","b",NA,"z|0","a"), "")
#> [1] 6
which.min.key(list("X",1,NULL,"F","Z"))
#> [1] 4
which.min.key(as.character(c(NA, NA)))
#> integer(0)
which.min.key(NA_character_)
#> integer(0)
which.min.key(list())
#> integer(0)