OneBite.Dev - Coding blog in a bite size

split a string by a delimiter in R

Code snippet on how to split a string by a delimiter in R

  x <- "This is a string" 
  strsplit(x, " ") 

This code creates a string called “x” and sets it equal to “This is a string”. The strsplit() function takes two arguments; the first is the string to be split and the second is the delimiter which specifies where the string should be split. In this case, the delimiter is the space character, so the function will split the string “This is a string” wherever it encounters a space. The output of the function is a vector containing the words “This”, “is”, “a” and “string”.

r