get_text_item: Extract portion of a string defined be a separator

Description Usage Arguments Examples

View source: R/string_manipulation.R

Description

Extract portions of a string as defined by a separator such as a comma or whitespace. For example, in a vector strings of first and last name, such as "Bob Smith", the first name can be extracted as item number one and last name as item number two.

Usage

1
get_text_item(x, item = 2, sep = " ")

Arguments

x

A vector of character strings

item

The relative position of the item to extract

sep

The separator used to split the string

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Define a vector of names that need to be broken out into columns
fish_names = tibble::tibble(names = c("Coho Salmon, Silvers",
                                      "Chinook Salmon, Kings"))

# Pull out the common name
fish_names$common_name = get_text_item(fish_names$names,
                                       item = 1, sep = " ")

# Pull out the alternate common name
fish_names$alternate_name = get_text_item(fish_names$names,
                                          item = 2, sep = ",")

# Trim whitespace from alternate_name
fish_names$alternate_name = trimws(fish_names$alternate_name, "b")

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