I have a mapping function as follows,
sub.func <- function(x,y){
if(agrepl(x,y)){
return(x)
}
else{
return(y)
}
}
now I want to apply this to compare two lists of different size, say myList1
and myList2
, such that for each element of myList1
, sub.func
will look for a match in myList2
and if yes will replace by the element of myList2
. So that all elements of myList2
are mapped to. How can I achieve this without using loops? Any variant of apply functions can be used?
e.g.
myList1 <- c("a b", "c d", "e f")
myList2 <- c("1", "a b d", "d", "e f g h", "2 3 a c d", "c g")
desired output is same length as myList2 but mapped to myList1 wherever possible
output <- c("1", "a b", "d", "e f", "c d", "c g")