Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
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

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 = '') 

Output: 

[1] "abc"

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

Browse Categories

...