pass vectors into a function using case_match


I would like to use case_match to replace a series of strings with another series of strings in a pairwise manner. For example,

x <- c("A", "B", "B")
x1 <- c("A", "B")
x2 <- c( "Apple", "Banana")

## would work like 
case_match(x, x1,x2)

## Instead of 

case_match(x, 'A'~'Apple', 
'B' ~ 'Banana')

Does this do what you want?

x <- c("A", "B", "B")
x1 <- c("A", "B")
x2 <- c( "Apple", "Banana")

CaseMatch <- function(Vec, Nms, Val) {
  names(Val) <- Nms
CaseMatch(x, x1, x2)
#>        A        B        B 
#>  "Apple" "Banana" "Banana"

Created on 2023-05-30 with reprex v2.0.2

While not what I was thinking, this does it and is pretty clever. Thanks

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.