OneBite.Dev - Coding blog in a bite size

split a string in R

Code snippet on how to split a string in R

  my_string <- "This is a test string."
  split_string <- strsplit(my_string, " ")

This code is used to split a string into individual words. The first line of code assigns the value “This is a test string.” to the variable my_string. The second line of code uses the strsplit function to take the my_string variable, and split it at each space into individual words, with the result being stored into the variable split_string. The strsplit function takes two arguments, the first being the string to be split, and the second argument being the character at which to perform the split. In this example the split character is a space, and thus the string is split into individual words.

r