Learn R Programming

qol (version 1.2.1)

sub_string: Retrieve A Substring From A Character

Description

sub_string() can extract parts of a character from the left side, right side or from the middle. It is also able to start or end at specific letter sequences instead of positions.

Usage

sub_string(data_frame, variable, from = NULL, to = NULL, case_sensitive = TRUE)

Value

Returns parts of a character vector.

Arguments

data_frame

A data frame which contains the variables to concatenate.

variable

A character variable to extract parts from.

from

The names of the variables to concatenate.

to

A single character which will be used to fill up the empty places.

case_sensitive

TRUE by default. When a character expression is passed as from or to it makes a difference whether a letter is written in upper or lower case. Pass FALSE to handle upper and lower case equaly.

See Also

Other character manipulating functions: concat(), remove_blanks()

Examples

Run this code
# Example data frame
my_data <- dummy_data(100)

# Extract text from the left
my_data[["left"]] <- my_data |> sub_string(education, to = 2)

# Extract text from the right
my_data[["right"]] <- my_data |> sub_string(education, from = 2)

# Extract text from the middle
my_data[["middle"]] <- my_data |> sub_string(education, from = 2, to = 3)

# Find text and extract from the left
my_data[["left2"]] <- my_data |> sub_string(education, to = "l")

# Find text and extract from the right
my_data[["right2"]] <- my_data |> sub_string(education, from = "l")

# Find text and extract from the middle
my_data[["middle2"]] <- my_data |> sub_string(education, from = "i", to = "l")

Run the code above in your browser using DataLab