grepInTable: Find a 3-letter country code in rworldmap::countrySynonyms

View source: R/grepInTable.R

grepInTableR Documentation

Find a 3-letter country code in rworldmap::countrySynonyms

Description

Search for a specific country name on different columns of alternative names in all but the first column of referenceTable and return the contents of the first column if found or "No match found for country", followed by the value of string for that case.

By default, referenceTable = countrySynonyms[, -1].

NOTE: This code will be offered to the maintainer of the rworldmap package. If they like it, it may not stay in Ecfun.

Usage

grepInTable(pattern, referenceTable = 
        rworldmap::countrySynonyms[, -1], 
        ignore.case=TRUE, collapse=', ', ...)

Arguments

pattern

Character vector of matches of pattern[i] in different columns of referenceTable[, -1] until a match is found.

referenceTable

data.frame with the first column containing reference codes to be returned if found.

if(ignore.case) force pattern and referenceTable toupper before searching for matches.

ignore.case

If TRUE, force all toupper.

collapse

Ignored if only one match is found. Otherwise, return paste0(referenceTable[ix, 1], collapse=collapse) where ix = the indices of all matches for pattern[i].

...

optional arguments to pass to grep.

Details

1. if(ignore.case) Force pattern and all columns of referenceTable toupper.

2. grep for string[i] in referenceTable[-1 and return the corresponding element of referenceTable[1] if found and an appropriate message otherwise.

Value

A character vector of the contents of referenceTable[1] for all matches or not-found messages that include string[i] when string[i] is not found.

Author(s)

Spencer Graves

See Also

grep, toupper, countrySynonyms

Examples

tstCodes <- grepInTable(
    c('Iran', 'Christmas Island', 'eSwatini'))
    
answer <- c(Iran='IRN', 'Christmas Island' =
  paste("Found row 53 of referenceTable with column 1 = ''"),  
  'eSwatini'="No match found for eSwatini")    


all.equal(tstCodes, answer)


sbgraves237/Ecfun documentation built on Dec. 3, 2024, 12:37 p.m.