Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sep argument in paste0() function behaves not as expected

I want to paste characters and separate them with certain sign, a , for example. Using paste() function works as expected:

paste("1st", "2nd", "3rd", sep = ", ")
[1] "1st, 2nd, 3rd"

But using the paste0() function results in a character with the separator only at the end:

paste0("1st", "2nd", "3rd", sep = ", ")
[1] "1st2nd3rd, "

I thought the main difference between both functions is that paste() makes a space by default and since I specified sep to be ", " in both examples I expect the same result. But this is not the case. Is this expected behaviour?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The paste0 function does not have a sep= parameter. It was specifically created to not use a separator. So these are all the same

paste0("1st", "2nd", "3rd", sep=", ")
paste0("1st", "2nd", "3rd",  ", ")
paste0(a="1st", b="2nd", c="3rd",  d=", ")

Check the ?paste0 help page for more info. If you want a separator, don’t use paste0. This is expected bahavior.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading