-1

In R I have a string like this:

'hello'

How do I convert it to a character vector like that:

[1] "h" "e" "l" "l" "o"
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
slartidan
  • 20,403
  • 15
  • 83
  • 131

2 Answers2

3

With stringr:

stringr::str_split("hello","")[[1]]
[1] "h" "e" "l" "l" "o"

Found another possible solution, although this is probably the worst approach:

substring("hello", seq(1,nchar("hello")), seq(1,nchar("hello")))
[1] "h" "e" "l" "l" "o"
RLave
  • 8,144
  • 3
  • 21
  • 37
0

While this might not be the most performant solution, this works as expected:

> unlist(strsplit('hello', ''))
[1] "h" "e" "l" "l" "o"

See the documentation of unlist and strsplit for further options.

slartidan
  • 20,403
  • 15
  • 83
  • 131