0 votes
1 view
in R Programming by (5.3k points)

If I have a vector of a type character, how can I concatenate the values into a string? Here's how I would do it with paste():

sdata = c('a', 'b', 'c')

paste(sdata[1], sdata[2], sdata[3], sep ='')

yielding "abc".

But of course, that only works if I know the length of sdata ahead of time

1 Answer

0 votes
by (25.3k points)

To concatenate characters of a vector, you can use the collapse argument inside the paste function as follows:

In your case:

sdata = c('a', 'b', 'c') 

paste(sdata, collapse = '') 


[1] "abc"

The collapse argument will return elements of the vector separated by the string you passed to “collapse” 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !