Consider
text <- "who let the dogs out"
fooo <- strsplit(text, " ")
fooo
[[1]]
[1] "who" "let" "the" "dogs" "out"
the output of strsplit
is a list. The list's first element then is a vector, that contains the words above.
Why does the function behave that way? Is there any case in which it would return a list with more than one element?
And I can access the words using
fooo[[1]][1]
[1] "who"
, but is there no simpler way?
The strsplit() method returns the list, where each list item resembles the item of input that has been split. The strsplit() method splits a character string or vector of character strings using a regular expression or a literal string and returns the substring list.
The strsplit() in R programming language function is used to split the elements of the specified character vector into substrings according to the given substring taken as its parameter.
To your first question, one reason that comes to mind is so that it can keep different length result vectors in the same object, since it is vectorized over x
:
text <- "who let the dogs out"
vtext <- c(text, "who let the")
##
> strsplit(text, " ")
[[1]]
[1] "who" "let" "the" "dogs" "out"
> strsplit(vtext, " ")
[[1]]
[1] "who" "let" "the" "dogs" "out"
[[2]]
[1] "who" "let" "the"
If this were to be returned as a data.frame
, matrix
, etc... instead of a list
, it would have to be padded with additional elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With