R: extract string before and after keyword - r

I want to extract the string before certain keywords and the first element right after the keyword. Given the following strings and the keywords,
s <- c("E123Apple12", "EJ23ZGrape0Z", "J8BananaZ!")
keywords <- c("Apple", "Grape", "Banana")
I would expect the output to be: E123, EJ23Z, and J8 for strings before the keywords, and 1, 0, and Z for the first element that appears right after the keyword.
Using sub(keywords, "\\1", s) gives me the following error:
Warning message:
In sub(keywords, "\\", s) :
argument 'pattern' has length > 1 and only the first element will be used

Your keywords need to be a regex string, rather than an R vector representing multiple matches. Then you can replace any matching keyword with an empty string, leaving just the characters around it:
keywords <- "(Apple|Grape|Banana)"
sub(keywords, "", s) # [1] "E123" "EJ23Z" "J8"
If you want just the characters before or after the keyword, you can match them with .*:
s <- c("E123AppleABC", "EJ23ZGrapeDEF", "J8BananaGHI")
keywords <- "(Apple|Grape|Banana).*"
sub(keywords, "", s) # [1] "E123" "EJ23Z" "J8"
keywords <- ".*(Apple|Grape|Banana)"
sub(keywords, "", s) # [1] "ABC" "DEF" "GHI"

If you have parallel vectors one way to do this would be to use strsplit, but you'll need to massage the result a little.
strsplit(s, keywords)
Results in:
[[1]]
[1] "E123" "12"
[[2]]
[1] "EJ23Z" "0Z"
[[3]]
[1] "J8" "Z!"
You need to select the first member in each list and combine into a vector like this:
unlist(lapply(strsplit(s, keywords), "[[", 1))
Which outputs
[1] "E123" "EJ23Z" "J8"
If you want what's after the string just do
unlist(lapply(strsplit(s, keywords), "[[", 2))

keywords <- "(Apple|Grape|Banana)"
sub(paste0("(.*)",keywords,".*"),'\\1',s)
[1] "E123" "EJ23Z" "J8"
sub(paste0(".*",keywords,"(\\w)",".*$"),'\\2',s)
[1] "1" "0" "Z"

for (x in keywords) {
s <- gsub(paste0("(.*)", x), "\\1", s)
}
s
# [1] "E123" "EJ23Z" "J8"

Related

find the longest consecutive set of vowels in a string

Trying to find the longest consecutive set of vowels in a string.
I have a very long string.
random_letters <- readr::read_lines("https://byuistats.github.io/M335/data/randomletters.txt")
I have stripped the string of all spaces and periods.
random_letters %>% str_replace_all(., fixed(" "), "") %>% str_replace_all('\\.', '')
I am now trying to find every single time a vowel, or combination of vowels occurs, and then identify the longest one.
so if the string looked like
string <- c("abcnduakngoaibhui")
the output would be
"a" "ua" "oai" "ui
In base R you could use strsplit:
strsplit(string, '[^aeiou]+')
[[1]]
[1] "a" "ua" "oai" "ui"
Also You could use str_extract_all from stringr package:
stringr::str_extract_all(string, "[aeiou]+")
[[1]]
[1] "a" "ua" "oai" "ui"
These gives you a list, since they are vectorized meaning that the string can be a vector:
string <- c("abcnduakngoaibhui", "aeityuioaiii")
strsplit(string, '[^aeiou]+')
[[1]]
[1] "a" "ua" "oai" "ui"
[[2]]
[1] "aei" "uioaiii"

Accessing element of a split string in R

If I have a string,
x <- "Hello World"
How can I access the second word, "World", using string split, after
x <- strsplit(x, " ")
x[[2]] does not do anything.
As mentioned in the comments, it's important to realise that strsplit returns a list object. Since your example is only splitting a single item (a vector of length 1) your list is length 1. I'll explain with a slightly different example, inputting a vector of length 3 (3 text items to split):
input <- c( "Hello world", "Hi there", "Back at ya" )
x <- strsplit( input, " " )
> x
[[1]]
[1] "Hello" "world"
[[2]]
[1] "Hi" "there"
[[3]]
[1] "Back" "at" "ya"
Notice that the returned list has 3 elements, one for each element of the input vector. Each of those list elements is split as per the strsplit call. So we can recall any of these list elements using [[ (this is what your x[[2]] call was doing, but you only had one list element, which is why you couldn't get anything in return):
> x[[1]]
[1] "Hello" "world"
> x[[3]]
[1] "Back" "at" "ya"
Now we can get the second part of any of those list elements by appending a [ call:
> x[[1]][2]
[1] "world"
> x[[3]][2]
[1] "at"
This will return the second item from each list element (note that the "Back at ya" input has returned "at" in this case). You can do this for all items at once using something from the apply family. sapply will return a vector, which will probably be good in this case:
> sapply( x, "[", 2 )
[1] "world" "there" "at"
The last value in the input here (2) is passed to the [ operator, meaning the operation x[2] is applied to every list element.
If instead of the second item, you'd like the last item of each list element, we can use tail within the sapply call instead of [:
> sapply( x, tail, 1 )
[1] "world" "there" "ya"
This time, we've applied tail( x, 1 ) to every list element, giving us the last item.
As a preference, my favourite way to apply actions like these is with the magrittr pipe, for the second word like so:
x <- input %>%
strsplit( " " ) %>%
sapply( "[", 2 )
> x
[1] "world" "there" "at"
Or for the last word:
x <- input %>%
strsplit( " " ) %>%
sapply( tail, 1 )
> x
[1] "world" "there" "ya"
Another approach that might be a little easier to read and apply to a data frame within a pipeline (though it takes more lines) would be to wrap it in your own function and apply that.
library(tidyverse)
df <- data.frame(
greetings = c( "Hello world", "Hi there", "Back at ya" )
)
split_params = function (x, sep, n) {
# Splits string into list of substrings separated by 'sep'.
# Returns nth substring.
x = strsplit(x, sep)[[1]][n]
return(x)
}
df = df %>%
mutate(
'greetings' = sapply(
X = greetings,
FUN = split_params,
# Arguments for split_params.
sep = ' ',
n = 2
)
)
df
### (Output in RStudio Notebook)
greetings second_word
<chr> <chr>
Hello world world
Hi there there
Back at ya at
3 rows
###
With stringr 1.5.0, you can use str_split_i to access the ith element of a split string:
library(stringr)
x <- "Hello World"
str_split_i(x, " ", i = 2)
#[1] "World"
It is vectorized:
x <- c("Hello world", "Hi there", "Back at ya")
str_split_i(x, " ", 2)
#[1] "world" "there" "at"
x=strsplit("a;b;c;d",";")
x
[[1]]
[1] "a" "b" "c" "d"
x=as.character(x[[1]])
x
[1] "a" "b" "c" "d"
x=strsplit(x," ")
x
[[1]]
[1] "a"
[[2]]
[1] "b"
[[3]]
[1] "c"
[[4]]
[1] "d"

Splitting a string in R

Consider:
x<-strsplit("This is an example",split="\\s",fixed=FALSE)
I am surprised to see that x has length 1 rather than length 4:
> length(x)
[1] 1
like this, x[3] is null. But If I unlist, then:
> x<-unlist(x)
> x
[1] "This" "is" "an" "example"
> length(x)
[1] 4
only now x[3] is "an".
Why wasn't that list originally by length 4 so that elements can be accessed by indexing? This gives troubles to access the splitted elements, since I have to unlist first.
This allows strsplit to be vectorized for its input argument. For instance, it will allow you to split a vector such as:
x <- c("string one", "string two", "and string three")
into a list of split results.
You do not need to unlist, but rather, you can refer to the element by a combination of its list index and the vector index. For instance, if you wanted to get the second word in the second item, you can do:
> x <- c("string one", "string two", "and string three")
> y <- strsplit(x, "\\s")
> y[[2]][2]
[1] "two"
That's because strsplit generates a list containing each element (word).
Try
> x[[1]]
#[1] "This" "is" "an" "example"
and
> length(x[[1]])
#[1] 4

R: cut string of characters in vectors

let x be the vector
[1] "hi" "hello" "Nyarlathotep"
Is it possible to produce a vector, let us say y, from x s.t. its components are
[1] "hi" "hello" "Nyarl"
?
In other words, I would need a command in R which cuts strings of text to a given length (in the above, length=5).
Thanks a lot!
More obvious than substring to me would be strtrim:
> x <- c("hi", "hello", "Nyarlathotep")
> x
[1] "hi" "hello" "Nyarlathotep"
> strtrim(x, 5)
[1] "hi" "hello" "Nyarl"
substring is great for extracting data from within a string at a given position, but strtrim does exactly what you're looking for.
The second argument is widths and that can be a vector of widths the same length as the input vector, in which case, each element can be trimmed by a specified amount.
> strtrim(x, c(1, 2, 3))
[1] "h" "he" "Nya"
Use substring see details in ?substring
> x <- c("hi", "hello", "Nyarlathotep")
> substring(x, first=1, last=5)
[1] "hi" "hello" "Nyarl"
Last update
You can also use sub with regex
> sub("(.{5}).*", "\\1", x)
[1] "hi" "hello" "Nyarl"
A (probably) faster alternative is sprintf():
sprintf("%.*s", 5, x)
[1] "hi" "hello" "Nyarl"

How should I split and retain elements using strsplit?

What a strsplit function in R does is, match and delete a given regular expression to split the rest of the string into vectors.
>strsplit("abc123def", "[0-9]+")
[[1]]
[1] "abc" "" "" "def"
But how should I split the string the same way using regular expression, but also retain the matches? I need something like the following.
>FUNCTION("abc123def", "[0-9]+")
[[1]]
[1] "abc" "123" "def"
Using strapply("abc123def", "[0-9]+|[a-z]+") works here, but what if the rest of the string other than the matches cannot be captured by a regular expression?
Fundamentally, it seems to me that what you want is not to split on [0-9]+ but to split on the transition between [0-9]+ and everything else. In your string, that transition is not pre-existing. To insert it, you could pre-process with gsub and back-referencing:
test <- "abc123def"
strsplit( gsub("([0-9]+)","~\\1~",test), "~" )
[[1]]
[1] "abc" "123" "def"
You could use lookaround assertions.
> test <- "abc123def"
> strsplit(test, "(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)", perl=T)
[[1]]
[1] "abc" "123" "def"
You can use strapply from gsubfn package.
test <- "abc123def"
strapply(X=test,
pattern="([^[:digit:]]*)(\\d+)(.+)",
FUN=c,
simplify=FALSE)
[[1]]
[1] "abc" "123" "def"

Resources