Skip to contents

Performs a find replace on a vector by comparing to another vector. When no matches occur, the original value is retained.

Usage

find_replace(original, to.find, to.replace, no.match = "original")

Arguments

original

The vector to search

to.find

A vector that possibly contain matches to original. Must be same length as to.replace

to.replace

A vector of values to use when a match is found between original and to.find Must be same length as to.match

no.match

User defined value to return where there is no match. Default is to return original value.

Value

Returns a data frame showing the original vector and the replacement. If no match was found, the original be returned. Both columns are characters.

Examples

#E.g., Search "original" for "1", "2", "3", "4", and replace with "blue","green","purple","yellow"
original<-c(3,10,3,1,5,2)
to.find<-c(1,2,3,4)
to.replace<-c("blue","green","purple","yellow")
find_replace(original,to.find,to.replace)
#>      original replacement
#> [1,] "3"      "purple"   
#> [2,] "10"     "10"       
#> [3,] "3"      "purple"   
#> [4,] "1"      "blue"     
#> [5,] "5"      "5"        
#> [6,] "2"      "green"    
find_replace(original,to.find,to.replace, no.match = 'NA')
#>      original replacement
#> [1,] "3"      "purple"   
#> [2,] "10"     NA         
#> [3,] "3"      "purple"   
#> [4,] "1"      "blue"     
#> [5,] "5"      NA         
#> [6,] "2"      "green"