# Unnamed:
test <- as.numeric(1:3)
stopifnot(identical(
match_names(test, c('a', 'c', 'b')),
c(a = 1, c = 2, b = 3)
))
# Named, reordered:
test <- c(c = 1, b = 2, a = 3)
stopifnot(identical(
match_names(test, c('a', 'c', 'b')),
c(a = 3, c = 1, b = 2)
))
# Default value specified by default= assigned to a
test <- c(c = 1, b = 2)
stopifnot(identical(
match_names(test, c('a', 'c', 'b'), NA),
c(a = NA, c = 1, b = 2)
))
# Default value specified in v assigned to a and b:
test <- c(c = 1, 2)
stopifnot(identical(
match_names(test, c('a', 'c', 'b')),
c(a = 2, c = 1, b = 2)
))
# Partial matching
test <- c(c = 1, 2)
stopifnot(identical(
match_names(test, c('a', 'cab', 'b')),
c(a = 2, cab = 1, b = 2)
))
# Multiple matching
test <- c(c = 1, 2, c = 3)
stopifnot(identical(
match_names(test, c('a', 'c', 'c')),
c(a = 2, c = 1, c = 3)
))
# Partial + multiple matching caveat: exact match will match first.
test <- c(c = 1, a = 2, ca = 3)
stopifnot(identical(
match_names(test, c('a', 'ca', 'ca')),
c(a = 2, ca = 3, ca = 1)
))
Run the code above in your browser using DataLab