I have a string :
string1 <- "This is my string"
I would like to convert it to a vector that looks like this:
vector1 "This" "is" "my" "string"
How do I do this? I know I could use the tm
package to convert to termDocumentMatrix
and then convert to a matrix but it would alphabetize the words and I need them to stay in the same order.
To convert a given character vector into integer vector in R, call as. integer() function and pass the character vector as argument to this function. as. integer() returns a new vector with the character values transformed into integer values.
To convert String to Integer in R programming, call strtoi() function, pass the string and base values to this function. strtoi(string, base) returns the integer value of the given string with respect to the specified base.
Note that splitting into single characters can be done via split = character(0) or split = "" ; the two are equivalent.
You can use strsplit to accomplish this task.
string1 <- "This is my string" strsplit(string1, " ")[[1]] #[1] "This" "is" "my" "string"
Slightly different from Dason, but this will split for any amount of white space including newlines:
string1 <- "This is my string" strsplit(string1, "\\s+")[[1]]
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