Sushant Shelke
Sushant Shelke

Reputation: 57

How to restructure characters in custom format?

i want to convert "hello,world" into vector as "hello","world"

i tried splitting as below:

c<- "hello,world"

spl<-unlist(strsplit(c, ","))

but I am getting result as below:

"hello" "world"

I want my result as:

"hello","world"

Upvotes: 0

Views: 45

Answers (2)

Vinay
Vinay

Reputation: 253

First make your string to a vector as follows.

cc<- str_split(cc,",")[[1]]

#Then use shQuote() to make each element of vector quoted string and then use paste() to make them comma separated.

print(paste(shQuote(cc), collapse=", "))

Upvotes: 0

ThomasIsCoding
ThomasIsCoding

Reputation: 102349

Do you mean this?

> cat(gsub("(\\w+)", "\"\\1\"", s), "\n")
"a","b","c","d"

Data

s <- "a,b,c,d"

Upvotes: 1

Related Questions