I would like to split a string into its elements and then paste them together again. However no matter what I tried, it doesn`t work.
Here are two things I’ve tried:
'hello' %>% strsplit('') %>% paste0()
output: "c(\"h\", \"e\", \"l\", \"l\", \"o\")"
'hello' %>% strsplit('') %>% unlist() %>% paste0()
output: "h" "e" "l" "l" "o"
I would simply like to get my ‘hello’ back.
>Solution :
You can use paste but you have to specify the collapse argument.
'hello' %>% strsplit('') %>% unlist() %>% paste(collapse = "")
Alternatively you can use str_c from the stringr library:
'hello' %>% strsplit('') %>% unlist() %>% stringr::str_c(collapse = "")