OneBite.Dev - Coding blog in a bite size

split a string by comma sign in R

Code snippet on how to split a string by comma sign in R

  str <- "Ned,Arya,Sansa,Bran"

  strsplit(str,',')

This code begins by setting a variable, str, equal to a string of names separated by commas. The second line uses the strsplit() command to split the string and return a vector, where each element is a separate name. The second argument passed to the strsplit() command is the character we want to use to split the string, in this case, the comma. The strsplit() command will return a vector with each name as a separate element.

r