How to Split Each Character of a String in R

To split each character of a string in R, you can use the strsplit() function and pass an empty string as a delimiter, which will split each character. E.g., the strsplit(string, split=””) function returns each string character.


rs <- ("This12is3First4R5String6Example")
strsplit(rs, split = "")


[1] "T" "h" "i" "s" "1" "2" "i" "s" "3" "F" "i" "r" "s" "t" "4" "R" "5" "S" "t"
[20] "r" "i" "n" "g" "6" "E" "x" "a" "m" "p" "l" "e"

In the above code example, we created a character string rs and split it into individual characters using R’s strsplit() function.

The strsplit() function takes two arguments: the first is the string to be split, and the second is the splitting pattern or separator. In this case, the splitting pattern is an empty string(“”), which means that the function will split the input string into individual characters.

After running this code, the output will be a list of individual characters, where each element corresponds to a character in the original string.

The resulting list can be assigned to a new variable for further processing or analysis. For example, this code helps manipulate and analyze character strings in R.

Leave a Comment