Enjoy R

Working with strings

R has not implemented a direct way to extract characters from a string by specifying their positions. It’s because R doesn’t effectively consider a string as a vector so you cannot use the operator [ ] to manage the characters it is composed of.
Some functions (like the ones implemented in the package stringr) helps to solve some of this problems; next function has an analogue task.

ch <- function(string, start = 1, end = NULL, from.end = F) {

    x <- unlist(strsplit(string, ""))

    if (!from.end) {
        if (!is.null(end)) {
            paste0(x[start:end], collapse = "")
        } else paste0(x[start], collapse = "")

    } else {
        start <- length(x) + 1 - start
        if (!is.null(end)) {
            end <- length(x) + 1 - end
            paste0(x[start:end], collapse = "")
        } else paste0(x[start], collapse = "")
    }

}

So, if my string is “Hello World” and I want to extract some characters from it, I will apply the function as follows:

HelloW <- "Hello World"

ch(HelloW)  # I want the first character

## [1] "H"


ch(HelloW, start = 3)  # the third

## [1] "l"


ch(HelloW, end = 5)  # from the first to the fifth

## [1] "Hello"


ch(HelloW, start = 2, end = 4)  # from the first to the fourth

## [1] "ell"


ch(HelloW, from.end = T)  # the last

## [1] "d"


ch(HelloW, start = 2, end = 4, from.end = T)  # from the second to the fourth, starting from the end of the string

## [1] "lro"


ch(HelloW, start = 4, end = 2, from.end = T)  # from the fourth to the second, starting from the end of the string

## [1] "orl"

Now, let’s do something more interesting.

stringNAME <- "R cihtym si"
stringLENGTH <- nchar(stringNAME)

I want to reverse part of my string: from the third to the last character.

paste0(ch(stringNAME, end = 2), ch(stringNAME, end = stringLENGTH - 2, from.end = TRUE))

## [1] "R is mythic"

# or, maybe more easily:
paste0(ch(stringNAME, end = 2), ch(stringNAME, start = stringLENGTH, end = 3))

## [1] "R is mythic"

Let’s play a little bit:

stringOUTPUT <- paste0(ch(stringNAME, end = 2), ch(stringNAME, end = stringLENGTH - 
    2, from.end = TRUE))

string2 <- "This day is really fantastic."

paste0(gsub(pattern = "mythic", replacement = ch(string2, end = 2, start = 10, 
    from.end = TRUE), x = stringOUTPUT), "!")

## [1] "R is fantastic!"
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: