Text Processing Made Easy with {healthyR}’s sql_left(), sql_mid(), and sql_right() Functions in R

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

Are you tired of manually manipulating text data in R? Do you find yourself frequently needing to extract substrings from long strings or to grab just the first few characters of a string? If so, you’re in luck! The {healthyR} library has three functions that will make your text processing tasks much easier: sql_left(), sql_mid(), and sql_right().

Function

Here are the function calls, I will also make the source avilable in the same cell so steal this code!!

# LEFT
sql_left("text", 3)

sql_left <- function(.text, .num_char) {
    base::substr(.text, 1, .num_char)
}

# MID
sql_mid("this is some text", 6, 2)

sql_mid <- function(.text, .start_num, .num_char) {
    base::substr(.text, .start_num, .start_num + .num_char - 1)
}

# RIGHT
sql_right("this is some more text", 3)

sql_right <- function(.text, .num_char) {
    base::substr(.text, base::nchar(.text) - (.num_char-1), base::nchar(.text))
}

Example

Let’s start with sql_left(). This function is similar to the LEFT() function in SQL and Excel, in that it returns the specified number of characters from the beginning of a string. For example, if we have the string “Hello, world!”, and we want to grab just the first three characters, we can use sql_left() like this:

library(healthyR)
sql_left("Hello, world!", 3)
[1] "Hel"

This will return the string “Hel”.

Next up is sql_mid(). This function is similar to the SUBSTRING() and MID() functions in SQL and Excel, in that it returns a specified portion of a string. The first argument is the string itself, the second argument is the starting position of the substring, and the third argument is the length of the substring. For example, if we have the string “This is some text”, and we want to grab the two characters starting at position six, we can use sql_mid() like this:

sql_mid("This is some text", 6, 2)
[1] "is"

This will return the string “is”.

Finally, we have sql_right(). This function is similar to the RIGHT() function in SQL and Excel, in that it returns the specified number of characters from the end of a string. For example, if we have the string “This is some more text”, and we want to grab just the last three characters, we can use sql_right() like this:

sql_right("This is some more text", 3)
[1] "ext"

This will return the string “ext”.

These three functions can be extremely helpful when working with text data in R. They can save you time and effort, and make your code more concise and readable. So next time you find yourself needing to manipulate text data, remember to reach for sql_left(), sql_mid(), and sql_right()!

Voila!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)