OneBite.Dev - Coding blog in a bite size

split a string by empty space in R

Code snippet on how to split a string by empty space in R

myString <- "This is a string"

splitString <- unlist(strsplit(myString, " "))

This code will split the provided string (in this case, “This is a string”) into individual words separated by empty space. To do this, we first create a variable (myString) to store the string. We then use the unlist and strsplit functions to store the individual words in a new variable (splitString). The strsplit function takes the string as its first argument and the type of split (in this case, empty space) as its second argument. The unlist function is used to convert the split string into a vector, making it easier to work with. After this code is run, the splitString variable contains the words from the original string as individual items in a vector.

r