## Tuesday, August 14, 2018

### (R) Modifying Strings with “stringr”

As the previous "R" articles have primarily addressed qualitative data assessment, I feel that at this time, that it is more than appropriate to discuss string manipulation within the “R” platform. This article will be demonstrate the functions which are available within the “stringr” package. This package provides numerous functional additions which allow for enhanced string manipulation.

Modify Punctuation

# Demo Set A #

DemoA <- c('The white and black cow')

# Demo Set B #

DemoB <- c('Jumped over the moon')

Changing Cases of String Contents

# Modify an entire string to upper case #

a <- str_to_upper(DemoA)

# Modify an entire string to lower case #

b <- str_to_lower(DemoA)

# Modify an entire string to resemble a book title #

c <- str_to_title(DemoA)

# View modifications #

print(a)

print(b)

print(c)

Console Output:

> print(a)[1] "THE WHITE AND BLACK COW"
> print(b)
[1] "the white and black cow"
> print(c)
[1] "The White And Black Cow"

Concatenate Strings

# Concatenate a String Variable #

a <- str_c(DemoA, DemoB)

# Join strings but separate joined values with " " #

b <- str_c(DemoA, DemoB, sep = " ")

# View modifications #

print(a)

print(b)

Console Output:

> print(a)
[1] "The white and black cowJumped over the moon"
> print(b)
[1] "The white and black cow Jumped over the moon"

Counting and Locating Sting Elements

# Count the spaces within a string variable #

a <- str_count(DemoA, pattern = " ")

# Extract string aspect which matches the string variable: “black” #

b <- str_extract (DemoA, "black")

# Count the length of a string in its entirety (includes spaces) #

c <- str_length(DemoA)

# View Outputs #

print(a)

print(b)

print(c)

Console Output:

> print(a)
[1] 4
> print(b)
[1] "black"
> print(c)
[1] 23

Removing String Aspects

# Remove the spaces within a string variable #

a <- str_remove_all(DemoA, " ")

# Replace one character within a string variable with a different character #

b <- str_replace_all(DemoA, " ", "-")

# View Outputs #

print(a)

print(b)

Console Output:

> print(a)
[1] "Thewhiteandblackcow"
> print(b)
[1] "The-white-and-black-cow"

Trimming String Elements

# Remove Un-necessary Whitespace #

DemoB <- c('Jumped over the moon ')

a <- str_trim(DemoB, side = c("right"))

DemoB <- c(' Jumped over the moon')

b <- str_trim(DemoB, side = c("left"))

print(b)

DemoB <- c(' Jumped over the moon ')

c <- str_trim(DemoB, side = c("both"))

print(a)

print(b)

print(c)

Console Output:

> print(a)
[1] "Jumped over the moon"
> print(b)
[1] "Jumped over the moon"
> print(c)
[1] "Jumped over the moon"

Sorting String Arrays

# Sort a string array alphabetically #

fruits <- c('apple', 'oranges', 'cherry', 'grape')

a <- str_sort(fruits)

# Sort a string array in reverse alphabetical order #

b <- str_sort(fruits, decreasing = TRUE)

print(a)

print(b)

Console Output:

> print(a)
[1] "apple" "cherry" "grape" "oranges"
> print(b)
[1] "oranges" "grape" "cherry" "apple"