find_nth_match: Find the nth match of one string in another string

Description Usage Arguments Value Warning Examples

View source: R/string_manipulation.R

Description

Identify the location of a specific match among several possible matches. Adapted from a stackoverflow solution by Abdelmonem Mahmoud.

Usage

1
find_nth_match(x, str_part, start_pos = 1, nth_match = 1)

Arguments

x

A vector of character strings to search

str_part

A character or string to search for

start_pos

Position in the string where the search should start

nth_match

The specific match to locate, such as match number 2

Value

An integer value indicating the position in the string where the nth_match was found, or NA if no nth_match was found.

Warning

Use with caution. This function has not been fully tested. Be careful using regular expressions for matching. Instead, define the character or string that you need to match by enclosing in quotes. For example "9", or "bb". To find special characters that need to be escaped, place two backslashes directly in front of the character. For example, "\\." to match a period.

Examples

1
2
3
4
5
6
7
8
# Create a vector of coordinate strings with transcription errors
dat = tibble::tibble(coords = c("47.1089N: -122.8965", "47.907N: -123.65.98"))

# Locate any unwanted entries of N
dat$dec_pos = find_nth_match(dat$coords, "N", nth_match = 1)

# Locate the extra decimal error
dat$dec_pos = find_nth_match(dat$coords, "\\.", nth_match = 3)

arestrom/remisc documentation built on July 16, 2020, 8:48 a.m.