2

I have a string like "abcde" and want it split into a vector like

> c("a", "b", "c", "d", "e")
[1] "a" "b" "c" "d" "e"

I found one way that I'll post as an answer but I am hoping someone else has a simpler way to do this, either in base R or using a package.

Sam Firke
  • 21,571
  • 9
  • 87
  • 105

1 Answers1

3

Using the stringr package:

library(stringr)
as.vector(str_split_fixed(x, pattern = "", n = nchar(x)))
[1] "a" "b" "c" "d" "e"

str_split_fixed produces a matrix that has to be coerced into a vector.

Sam Firke
  • 21,571
  • 9
  • 87
  • 105
  • 1
    stringr 1.5.0 introduced `str_split_1()`, which splits a single string into a character vector – acvill May 03 '23 at 20:44