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
Related
This question already has answers here:
Select first element of nested list
(5 answers)
R list get first item of each element
(2 answers)
Closed 3 years ago.
I have a splitted string of a vector like
df <- c("Test A:No1", "Test B:No2")
l <- str_split(df, ":")
l
which returns me
[[1]]
[1] "Test A" "No1"
[[2]]
[1] "Test B" "No2"
Now I am interested in accessing all first elements and all last elements independently or create a vector like
[1] "Test A" "Test B"
and
[1] "No1" "No2"
I tried several types of single and double brackets, with and without commas, but l[[x]][1] or l[[x]][2] give me only the list element x.
How can I access all elements at once (e.g. l[[]][1] )?
You may use sapply.
sapply(l, `[`, 1)
# [1] "Test A" "Test B"
sapply(l, `[`, 2)
# [1] "No1" "No2"
Explanation: In R quite everything is a function. Also the parentheses `[` actually are functions. Considering following example makes clear why the sapply above works.
Example
Consider this vector
x <- c("A", "B")
Whey we're doing
x[1]
# [1] "A"
x[2]
# [2] "B"
we're actually applying the special form of the underlying prefix-form of the `[` function:
`[`(x, 1)
# [1] "A"
`[`(x, 2)
# [1] "B"
maybe using unlist and lapply can get the work done.
df <- c("Test A:No1", "Test B:No2")
l <- str_split(df, ":")
> unlist(lapply(l,function(x) x[1]))
[1] "Test A" "Test B"
> unlist(lapply(l,function(x) x[length(x)]))
[1] "No1" "No2"
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"
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"
Say I have a character vector like:
x <- c('A__B__Mike','A__Paul','Daniel','A__B__C__Martha','A__John','A__B__C__D__Laura')
I want a vector of only the names in the last position; I guess I could do it removing the first chunk using regular expressions, but say I want to use strsplit() to split by '__':
x.list <- strsplit(x, '__')
How would I access the last subelement (the names) of each element in this list? I only know how to do it if I know the position:
sapply(x.list, "[[", 1)
But how to access the last when the position is variable? Thanks!
In any case, what would be the fastest way to extract the names out of x in the first place? Anything faster than the strsplit approach?
We can do this with base R. Either using sub
sub(".*__", "", x)
#[1] "Mike" "Paul" "Daniel" "Martha" "John" "Laura"
or with strsplit, we get the last element with tail
sapply(strsplit(x, '__'), tail, 1)
#[1] "Mike" "Paul" "Daniel" "Martha" "John" "Laura"
Or to find the position, we can use gregexpr and then extract using substring
substring(x, sapply(gregexpr("[^__]+", x), tail, 1))
#[1] "Mike" "Paul" "Daniel" "Martha" "John" "Laura"
Or with stri_extract_last
library(stringi)
stri_extract_last(x, regex="[^__]+")
#[1] "Mike" "Paul" "Daniel" "Martha" "John" "Laura"
Use word function of stringr package
library(stringr)
word(x,start = -1,sep = "\\_+")
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"